HDU 2066 一个人的旅行 spfa
2017-03-05 09:59
531 查看
http://acm.hdu.edu.cn/showproblem.php?pid=2066
【思路】
只是去到某个喜欢的城市,那么我们再填个源点,与草儿能直接到的城市建一条边权为0的边,剩下的就是求一次单源最短路了.
【思路】
只是去到某个喜欢的城市,那么我们再填个源点,与草儿能直接到的城市建一条边权为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); } }
相关文章推荐
- spfa hdu 2066 一个人的旅行
- HDU 2066 一个人的旅行(spfa)
- HDU 2066 一个人的旅行(单源最短路SPFA)
- 【HDU - 2066 一个人的旅行】 最短路 dijkstra,spfa
- hdu-2066 一个人的旅行(SPFA做法)
- HDU 2066 一个人的旅行(spfa)
- hdu 2066 一个人的旅行(最短路+SPFA)
- hdu-2066 一个人的旅行(最短路spfa)
- HDU——2066一个人的旅行(优先队列SPFA水题)
- HDU 2066 一个人的旅行【最短路 dijkstra & floyed & SPFA 】
- HDU 2066:一个人的旅行【Dijkstra & SPFA & Floyd】
- 一个人的旅行(http://acm.hdu.edu.cn/showproblem.php?pid=2066)SPFA||dijkstra
- hdu 2066 一个人的旅行-spfa
- HDU 2066 一个人的旅行(SPFA +Floyd-Warshall算法)
- HDU 2066 一个人的旅行 (dijstra bellman-ford flyod SPFA)
- HDU 2066 一个人的旅行 dijkstra&&spfa
- hdu 2066 一个人的旅行 SPFA
- HDU 2066 一个人的旅行(SPFA +Floyd-Warshall算法)
- hdu 2066 一个人的旅行 最短路spfa
- hdu 2066 一个人的旅行 最短路