1. 程式人生 > >一個人的旅行 HDU - 2066 (最短路)

一個人的旅行 HDU - 2066 (最短路)

mis 一個人 ssi 輸出 int cstring col 個數 cto

一個人的旅行

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 50701 Accepted Submission(s): 16857


Problem Description 雖然草兒是個路癡(就是在杭電待了一年多,居然還會在校園裏迷路的人,汗~),但是草兒仍然很喜歡旅行,因為在旅途中 會遇見很多人(白馬王子,^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

思路:最短路的題目,用floyd 會超時的,用dijkstra就可以,只需要把家看作0,並且將相鄰的那些火車站的cost邊權看為0就好了(cost[0][u]=0)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include
<queue> #include<set> #include<map> #include<vector> using namespace std; #define INF 0x3f3f3f3f #define eps 1e-10 typedef long long ll; const int maxn = 1e3+5; const int mod = 1e9 + 7; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } int cost[maxn][maxn];
int d[maxn]; bool used[maxn]; int T,n; void dijk(int s) { fill(d,d+1002,INF); fill(used,used+1002,false); d[s]=0; while(true) { int v=-1; for(int u=0;u<=n;u++) { if(!used[u]&&(v==-1||d[u]<d[v])) v=u; } if(v==-1) break; used[v]=true; for(int u=0;u<=n;u++) d[u]=min(d[u],d[v]+cost[v][u]); } } int main() { int S,D; while(cin>>T>>S>>D) { memset(cost,INF,sizeof(cost)); for(int i=0;i<1002;i++) cost[i][i]=0; memset(d,INF,sizeof(d)); n=0; while(T--) { int a,b,l; cin>>a>>b>>l; n=max(max(a,b),n); if(l<cost[a][b]) { cost[a][b]=l; cost[b][a]=l; } } while(S--) { int a; cin>>a; cost[0][a]=0; cost[a][0]=0; } dijk(0); int ans=INF; for(int i=0;i<D;i++) { int end; cin>>end; ans=min(ans,d[end]); } cout<<ans<<endl; } }

一個人的旅行 HDU - 2066 (最短路)