标签:
最短路一个人的旅行
Description
Input
Output
Sample Input
Sample Output
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<algorithm> 6 7 using namespace std; 8 9 #define MAX 0x3f3f3f3f 10 11 int road,link,want,total; 12 int map[1005][1005],linkarr[1005],wantarr[1005],dis[1005]; 13 bool visit[1005]; 14 15 void Dijkstra(int start)///起始点 16 { 17 int temp,k; 18 memset(visit,0,sizeof(visit)); 19 20 for(int i=1;i<=total;++i) 21 dis[i]=map[start][i]; 22 23 dis[start]=0; 24 visit[start]=1; 25 26 for(int i=1;i<=total;++i) 27 { 28 temp=MAX; 29 for(int j=1;j<=total;++j) 30 if(!visit[j]&&dis[j]<temp) 31 temp=dis[k=j]; 32 33 visit[k]=1; 34 for(int j=1;j<=total;++j) 35 if(!visit[j]&&dis[k]+map[k][j]<dis[j]) 36 dis[j]=dis[k]+map[k][j]; 37 } 38 } 39 40 int main() 41 { 42 int x,y,cost,minn,answer; 43 while(scanf("%d%d%d",&road,&link,&want)!=EOF) 44 { 45 total=0; 46 memset(map,MAX,sizeof(map)); 47 48 for(int i=1;i<=road;++i) 49 { 50 scanf("%d%d%d",&x,&y,&cost); 51 if(cost<map[x][y]) 52 map[x][y]=map[y][x]=cost; 53 54 total=max(total,max(x,y)); 55 } 56 57 for(int i=1;i<=link;++i) //相连的城市 58 scanf("%d",&linkarr[i]); 59 60 for(int i=1;i<=want;++i) //目的地 61 scanf("%d",&wantarr[i]); 62 63 answer=MAX; 64 for(int i=1;i<=link;++i) //linkarr数组中所有元素中到达目的地最短的路 65 { 66 Dijkstra(linkarr[i]); 67 minn=MAX; 68 for(int j=1;j<=want;++j) //linkarr[i]中可以到达的目的地中最短 69 if(dis[wantarr[j]]<minn) 70 minn=dis[wantarr[j]]; 71 if(answer>minn) 72 answer=minn; 73 } 74 75 printf("%d\n",answer); 76 } 77 return 0; 78 }
标签:
原文地址:http://www.cnblogs.com/moqitianliang/p/4685317.html