http://acm.hdu.edu.cn/showproblem.php?pid=2066
- 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景……草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女……眼看寒假就快到了,这么一大段时间,可不能浪费啊,一定要给自己好好的放个假,可是也不能荒废了训练啊,所以草儿决定在要在最短的时间去一个自己想去的地方!因为草儿的家在一个小镇上,没有火车经过,所以她只能去邻近的城市坐火车(好可怜啊~)。
-
Input
输入数据有多组,每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个,草儿想去的地方有D个;
接着有T行,每行有三个整数a,b,time,表示a,b城市之间的车程是time小时;(1=<(a,b)<=1000;a,b 之间可能有多条路)
接着的第T+1行有S个数,表示和草儿家相连的城市;
接着的第T+2行有D个数,表示草儿想去地方。 -
Output
输出草儿能去某个喜欢的城市的最短时间。
-
Sample Input
6 2 3 1 3 5 1 4 7 2 8 12 3 8 4 4 9 12 9 10 2 1 2 8 9 10
-
Sample Output
9
-
思路:反向建边 or 建超级源 + 最短路
- 代码:
-
#include <bits/stdc++.h> using namespace std; const int maxn=1e5; const int maxm=1e7; const int INF= 0x3f3f3f3f; int head[maxn],d[maxn]; int cnt=0; struct node { int to,w,next; }eg[maxm]; typedef pair <int , int > P; void add(int u,int v,int w) { eg[cnt].to=v; eg[cnt].w=w; eg[cnt].next=head[u]; head[u]=cnt++; } void dijkstra(int s) { priority_queue<P,vector<P>,greater<P> >que; d[s]=0; que.push(P(0,s)); while(!que.empty()) { P p = que.top(); que.pop(); int v=p.second; if(d[v]<p.first) continue; for(int i=head[v];i!=-1;i=eg[i].next) { node e=eg[i]; if(e.w+d[v]<d[e.to]) { d[e.to]=e.w+d[v]; que.push(P(d[e.to],e.to)); } } } } int main() { int T,S,D; while(~scanf("%d%d%d",&T,&S,&D)) { memset(head,-1,sizeof(head)); memset(d,INF,sizeof(d)); while(T--) { int a,b,c; cin>>a>>b>>c; add(a,b,c); add(b,a,c); } while(S--) { int s; cin>>s; add(0,s,0); } dijkstra(0); int MIN=INF; while(D--) { int t; cin>>t; MIN=min(d[t],MIN); } cout<<MIN<<endl; } }