题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066
题目大意:这是一个不同于其他最短路模板的题目,他特别的地方是起点不同,起点有不同个,来求到达终点的最短路。注意时间问题,以防超时~~
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <cmath> 6 using namespace std; 7 int n,Min,MM,map[1010][1010],d,node[1010],p[1010],q[1010]; 8 const int INF=999999; 9 10 void set () 11 { 12 for (int i=1; i<1010; i++) 13 { 14 node[i]=INF; 15 for (int j=1; j<1010; j++) 16 map[i][j]=INF; 17 } 18 } 19 20 int dijkstra(int m) 21 { 22 int vis[1010]= {0}; 23 int tm=m; 24 vis[m]=1; 25 node[m]=0; 26 for (int k=2; k<=n; k++) 27 { 28 Min=INF; 29 for (int i=1; i<=n; i++) 30 if (!vis[i]) 31 { 32 if (node[i]>map[tm][i]+node[tm]) 33 node[i]=map[tm][i]+node[tm]; 34 if (Min>node[i]) 35 { 36 Min=node[i]; 37 m=i; 38 } 39 } 40 vis[m]=1; 41 tm=m; 42 for (int j=1; j<=d; j++) 43 if (q[j]==tm) 44 return Min; 45 } 46 return -1; 47 } 48 49 int main () 50 { 51 int T,s; 52 while (~scanf("%d%d%d",&T,&s,&d)) 53 { 54 //cout<<d<<endl; 55 set(); 56 n=0; 57 //cout<<d<<endl; 58 while (T--) 59 { 60 int a,b,t; 61 scanf("%d%d%d",&a,&b,&t); 62 //cout<<1<<endl; 63 if (a>n) 64 n=a; 65 else if (b>n) 66 n=b; 67 if (map[a][b]>t) 68 map[a][b]=map[b][a]=t; 69 } 70 for (int i=1; i<=s; i++) 71 scanf("%d",&p[i]); 72 for (int j=1; j<=d; j++) 73 scanf("%d",&q[j]); 74 MM=INF; 75 for (int i=1; i<=s; i++) 76 { 77 int w=dijkstra(p[i]); 78 if (MM>w&&w!=-1) 79 MM=w; 80 } 81 printf ("%d\n",MM); 82 } 83 return 0; 84 }
hdu 2066 一个人的旅行,布布扣,bubuko.com
原文:http://www.cnblogs.com/qq-star/p/3916249.html