标签:
Description
Input
Output
Sample Input
Sample Output
1 # include<iostream> 2 # include<cstdio> 3 # include<cstring> 4 # include<queue> 5 # include<algorithm> 6 using namespace std; 7 const int INF=1<<30; 8 struct edge 9 { 10 int to,w,nxt; 11 }; 12 edge e[2005]; 13 int head[1005],dis[1005]; 14 int t,s,d,cnt,sta[1005],ed[1005]; 15 void add(int a,int b,int c) 16 { 17 e[cnt].to=b; 18 e[cnt].w=c; 19 e[cnt].nxt=head[a]; 20 head[a]=cnt++; 21 } 22 int spfa(int from,int to) 23 { 24 fill(dis,dis+1004,INF); 25 dis[from]=0; 26 queue<int>q; 27 q.push(from); 28 while(!q.empty()){ 29 int u=q.front(); 30 q.pop(); 31 for(int i=head[u];i!=-1;i=e[i].nxt){ 32 if(dis[e[i].to]>dis[u]+e[i].w){ 33 dis[e[i].to]=dis[u]+e[i].w; 34 q.push(e[i].to); 35 } 36 } 37 } 38 return dis[to]; 39 } 40 int main() 41 { 42 int i,j,a,b,c; 43 while(scanf("%d%d%d",&t,&s,&d)!=EOF) 44 { 45 cnt=0; 46 memset(head,-1,sizeof(head)); 47 for(i=1;i<=t;++i){ 48 scanf("%d%d%d",&a,&b,&c); 49 add(a,b,c); 50 add(b,a,c); 51 } 52 for(i=0;i<s;++i) 53 scanf("%d",&sta[i]); 54 for(i=0;i<d;++i) 55 scanf("%d",&ed[i]); 56 int ans=INF; 57 for(i=0;i<s;++i) 58 for(j=0;j<d;++j) 59 ans=min(ans,spfa(sta[i],ed[j])); 60 printf("%d\n",ans); 61 } 62 return 0; 63 }
标签:
原文地址:http://www.cnblogs.com/20143605--pcx/p/4677202.html