您的位置:首页 > 其它

HDU 2066 一个人的旅行 spfa

2017-03-05 09:59 531 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2066

【思路】

只是去到某个喜欢的城市,那么我们再填个源点,与草儿能直接到的城市建一条边权为0的边,剩下的就是求一次单源最短路了.

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;

const int Mx=1010;
const int inf=0x3f3f3f3f;
int d[Mx],head[Mx];
int cnt,start,S,T,D;
bool v[Mx];
struct node
{
int from,to,v,next;
} p[Mx*100];

void add(int u,int v,int w)
{
p[cnt].from= u;
p[cnt].to= v;
p[cnt].v= w;
p[cnt].next= head[u];
head[u]= cnt++;
}
void spfa(int s)
{
for(int i=0;i<Mx;i++) d[i]=inf;
memset(v,false,sizeof(v));
queue<int> q;
d[s]=0;v[s]=true;
q.push(s);
while (!q.empty())
{
int u=q.front();
q.pop();
v[u]=true;
for (int i=head[u];i!=-1;i=p[i].next)
{
int t=p[i].to;
if (d[t]>d[u]+p[i].v)
{
d[t]=d[u]+p[i].v;
if (!v[t])
{
v[t]=true;
q.push(t);
}
}
}
v[u]=false;
}
}

int main()
{
int x,y,z,ans;
while(~scanf("%d%d%d",&S,&T,&D))
{
cnt=0;
memset(head, -1, sizeof(head));
for (int i=1;i<=S;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
add(y,x,z);
}
for (int i=1;i<=T;i++)
{
scanf("%d",&x);
add(x,0,0);
add(0,x,0);
}
spfa(0);
ans=inf;
while (D--)
{
scanf("%d",&x);
ans=min(ans,d[x]);
}
printf("%d\n",ans);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  HDU spfa