标签:
Description
Input
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
1 #include<cstdio> 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #define INF 0xfffffff 6 using namespace std; 7 int t,s,d; 8 int pri[1111][1111]; 9 int vis[1111]; 10 int dis[1111]; 11 int be[1111],ed[1111]; 12 void dijkstra(int a,int b) 13 { 14 memset(vis,0,sizeof(vis)); 15 vis[a]=1; 16 for(int i = 1;i <= b;i++) 17 dis[i]=pri[a][i]; 18 for(int i = 1;i <= b;i++) 19 { 20 int M=INF,k=-1; 21 for(int j = 1;j <= b;j++) 22 { 23 if(!vis[j]&&dis[j]<M) 24 M=dis[j],k=j; 25 } 26 if(k==-1) 27 return ; 28 vis[k]=1; 29 for(int j = 1;j <= b;j++) 30 { 31 if(!vis[j]&&dis[j]>dis[k]+pri[k][j]) 32 dis[j]=dis[k]+pri[k][j]; 33 } 34 } 35 } 36 int main() 37 { 38 while(scanf("%d %d %d",&t,&s,&d)!=EOF) 39 { 40 for(int i = 1;i <= 1000;i++)//区间开到1111就超时,真的日狗 41 for(int j = 1;j <= 1000;j++) 42 pri[i][j]=i==j?0:INF; 43 int a,b,c,n=0; 44 for(int i = 1;i <= t;i++) 45 { 46 scanf("%d %d %d",&a,&b,&c); 47 if(max(a,b)>n) 48 n=max(a,b); 49 if(pri[a][b]>c) 50 pri[a][b]=pri[b][a]=c; 51 } 52 for(int i = 1;i <= s;i++) 53 scanf("%d",&be[i]); 54 for(int i = 1;i <= d;i++) 55 scanf("%d",&ed[i]); 56 int tim = INF; 57 for(int i = 1;i <= s;i++) 58 { 59 dijkstra(be[i],n); 60 for(int j = 1;j <= d;j++) 61 { 62 if(dis[ed[j]]<tim) 63 tim = dis[ed[j]]; 64 } 65 } 66 printf("%d\n",tim); 67 68 } 69 return 0; 70 }
标签:
原文地址:http://www.cnblogs.com/llal/p/5738254.html