题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7201 Accepted Submission(s):
2350
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int node[1010],map[1010][1010],n,Min; 6 const int INF=9999999; 7 8 void dijkstra() 9 { 10 int vis[1010]= {0}; 11 int tm=0,m; 12 node[tm]=0; 13 vis[tm]=1; 14 for(int i = 1; i <= n; i++) 15 node[i] = INF; 16 for (int k=2; k<=n; k++) 17 { 18 Min=INF; 19 for (int i=1; i<=n; i++) 20 if (!vis[i]) 21 { 22 if (node[i]>map[tm][i]+node[tm]) 23 node[i]=map[tm][i]+node[tm]; 24 if (Min>node[i]) 25 { 26 Min=node[i]; 27 m=i; 28 } 29 } 30 vis[m]=1; 31 tm=m; 32 } 33 } 34 35 int main () 36 { 37 int m,s; 38 while (~scanf("%d%d%d",&n,&m,&s)) 39 { 40 memset(map, INF, sizeof(map)); 41 for (int i=1; i<=m; i++) 42 { 43 int p,q,t; 44 cin>>p>>q>>t; 45 if (map[p][q]>t) 46 map[p][q]=t; 47 } 48 int w,cost; 49 cin>>w; 50 for (int i=1; i<=w; i++) 51 { 52 scanf ("%d",&cost); 53 map[0][cost]=0; 54 } 55 dijkstra(); 56 if(node[s] ==INF) 57 printf("-1\n"); 58 else 59 printf("%d\n", node[s]); 60 } 61 return 0; 62 }
hdu 2680 Choose the best route (dijkstra算法 最短路问题),布布扣,bubuko.com
hdu 2680 Choose the best route (dijkstra算法 最短路问题)
原文:http://www.cnblogs.com/qq-star/p/3925423.html