You and your friends are going to camping next weekend , because you are the best driver and you are living in this town for ten years , you have to drive the car from your home and pick up each of your friends to take them with you ,then finally go to the camping site. You have the map of the town and the length of every street ,and homes of friends will be at some intersections. you want to start from your home then pick up all of your friends then go to the camping site as fast as you can so you have to find the shortest path to do that. you can pick up your friends in any order but you have to make sure you pick up all of them. NOTE: you can pass by any intersection more than once including home and camping site .
题意 :求经过几个特定点的最短路,标记一下状态,然后跑最短路就好了。
卡了很久的原因。。。。他喵了居然会住在一起。。。。
#include <bits/stdc++.h>
using namespace std;
struct dd
{
int v,w;
};
int dp[510][2500],f[510][2500],f1[510];
int b[115]={1,2,4,8,16,32,64,128,256,512,1024,2048};
vector <dd> d[510];
struct no{
int i,v;
int fi[15];
int vi;
bool operator <(const no &a)const{
return v>a.v;
}
};
priority_queue < no > q;
int dk(int s,int e,int ko){
dp[s][0]=0;
no dk1;
while(!q.empty())q.pop();
dk1.i=s,dk1.v=0,dk1.vi=0;
for(int i=0;i<15;i++)dk1.fi[i]=-1;
q.push(dk1);
while(!q.empty()){
no p=q.top();
q.pop();
//if(p.i==e&&p.vi==ko)return dp[p.i][ko];
if(f[p.i][p.vi]==-1)f[p.i][p.vi]=1;
else continue;
for(int i=0;i<(int)d[p.i].size();i++){
int v=d[p.i][i].w,y=d[p.i][i].v;
no pp=p;
if(f1[y]!=-1&&p.fi[f1[y]]==-1){
pp.fi[f1[y]]=1;
pp.vi=p.vi+b[f1[y]];
}
if(dp[y][pp.vi]==-1||dp[y][pp.vi]>dp[p.i][p.vi]+v){
dp[y][pp.vi]=dp[p.i][p.vi]+v;
pp.v=dp[y][pp.vi];
pp.i=y;
q.push(pp);
}
}
}
return dp[e][ko];
}
void init(int n){
for(int i=1;i<=n;i++)d[i].clear();
memset(dp,-1,sizeof(dp));
memset(f1,-1,sizeof(f1));
memset(f,-1,sizeof(f));
}
int main()
{
int T;
cin>>T;
int m,n,k;
int s,e,v,p;
for(int ii=1;ii<=T;ii++)
{
scanf("%d%d%d",&m,&n,&k);
init(m);
for(int i=0;i<n;i++){
scanf("%d%d%d",&s,&e,&v);
d[s].push_back((dd){e,v});
d[e].push_back((dd){s,v});
}
int ans=0;
int top=0,u[110];
memset(u,-1,sizeof(u));
for(int i=0;i<k;i++){
scanf("%d",&p);
if(u[p]==-1){u[p]=1;
f1[p]=top;
ans+=b[top++];}
}
printf("Case %d: %d\n",ii,dk(1,m,ans));
}
return 0;
}
本文介绍了一种解决特定场景下的最短路径问题的算法实现,该问题涉及从起点出发,依次经过若干特定点最终到达终点的情况。通过状态标记和最短路径计算,确保能够以最高效的方式完成路径规划。
827

被折叠的 条评论
为什么被折叠?



