标签:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 28925 Accepted Submission(s):
9942
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #define M 1005 5 #define MAX 0x3f3f3f3f 6 using namespace std; 7 int map[M][M],vis[M],dis[M]; 8 int main() 9 { 10 int n,m,i,j,T,S,D; 11 while(~scanf("%d%d%d",&T,&S,&D)) 12 { 13 memset(vis,0,sizeof(vis)); 14 memset(dis,0,sizeof(dis)); 15 for(i=0; i<=1000; i++) 16 for(j=0; j<=1000; j++) 17 { 18 if(i==j) map[i][j]=0; 19 else map[i][j]=MAX; 20 } 21 int a,b,c; 22 n=0; 23 while(T--) 24 { 25 scanf("%d%d%d",&a,&b,&c); 26 if(a>n) n=a; 27 if(b>n) n=b; 28 if(map[a][b]>c) 29 map[a][b]=c,map[b][a]=c; 30 } 31 int k; 32 for(i=1; i<=S; i++) 33 { 34 scanf("%d",&k); 35 map[0][k]=0; //将小草的家看作城市0 36 map[k][0]=0; 37 } 38 vis[0]=1; 39 for(i=0; i<=n; i++) 40 dis[i]=map[0][i]; 41 int min,t; 42 for(i=1; i<=n; i++) 43 { 44 min=MAX; 45 for(j=1; j<=n; j++) 46 if(!vis[j]&&dis[j]<min) 47 { 48 min=dis[j]; 49 t=j; 50 } 51 52 vis[t]=1; 53 for(j=1; j<=n; j++) 54 if(!vis[j]&&map[j][t]<MAX) 55 if(dis[j]>dis[t]+map[j][t]) 56 dis[j]=dis[t]+map[j][t]; 57 } 58 min=MAX; 59 for(i=1; i<=D; i++) 60 { 61 scanf("%d",&k); 62 if(min>dis[k]) //寻找草儿想去的路程最短的城市 63 min=dis[k]; 64 } 65 printf("%d\n",min); 66 } 67 return 0; 68 }
标签:
原文地址:http://www.cnblogs.com/pshw/p/5371077.html