The shortest path to a man's journey
Description
Although the grass is a road fetish (that is, in Hangzhou electric for more than a year, incredibly still will be lost in the campus, Khan ~), but the grass is still very fond of travel, because in the journey will meet a lot of people (Prince Charming, ^0^), a lot of things, but also to enrich their experience, but also to see beautiful scenery ... Grass wants to go to many places, she wants to go to the Tokyo Tower to see the night scene, to Venice to see movies, to see the Taro on Yangmingshan, go to New York pure snow, go to Paris to drink coffee letter, to Beijing to visit Meng Jiangnu ... See the winter vacation is coming, so a large period of time, can not be wasted ah, must give yourself good put a fake, but also can not waste training ah, so grass son decided in the shortest time to a place where they want to go! Because the house of grass is in a small town, there is no train passing by, so she can only go to the neighboring city by train (poor Ah ~).
Input
The input data has more than one group, the first line of each group is three integers t,s and D, indicating that there is a T road, and the grass son of the city adjacent to the cities have s, where the grass wants to go there are D;Then there is a T-line, each line has three integer a,b,time, indicating that the drive between A and B cities is time hours, (1=< (A, B) there may be multiple routes between <=1000;a,b) the next t+1 line has s number, indicating the city connected with the grass family; the next line of T+2 has a number D, indicating that the grass wants to go to the place.
Output
The shortest time the grass can go to a favorite city.
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 <iostream>2#include <cstdio>3#include <cstring>4#include <string>5#include <algorithm>6 7 using namespacestd;8 9 #defineMAX 0x3f3f3f3fTen One introad,link,want,total; A intmap[1005][1005],linkarr[1005],wantarr[1005],dis[1005]; - BOOLvisit[1005]; - the voidDijkstra (intStart///starting point - { - inttemp,k; -memset (Visit,0,sizeof(visit)); + - for(intI=1; i<=total;++i) +dis[i]=Map[start][i]; A atdis[start]=0; -visit[start]=1; - - for(intI=1; i<=total;++i) - { -temp=MAX; in for(intj=1; j<=total;++j) - if(!visit[j]&&dis[j]<temp) totemp=dis[k=j]; + -visit[k]=1; the for(intj=1; j<=total;++j) * if(!visit[j]&&dis[k]+map[k][j]<Dis[j]) $dis[j]=dis[k]+Map[k][j];Panax Notoginseng } - } the + intMain () A { the intX,y,cost,minn,answer; + while(SCANF ("%d%d%d", &road,&link,&want)! =EOF) - { $Total=0; $memset (Map,max,sizeof(map)); - - for(intI=1; i<=road;++i) the { -scanf"%d%d%d",&x,&y,&Cost );Wuyi if(cost<Map[x][y]) themap[x][y]=map[y][x]=Cost ; - WuTotal=Max (Total,max (x, y)); - } About $ for(intI=1; i<=link;++i)//the Connected City -scanf"%d",&linkarr[i]); - - for(intI=1; i<=want;++i)//Destination Ascanf"%d",&wantarr[i]); + theAnswer=MAX; - for(intI=1; i<=link;++i)//Linkarr the shortest path to the destination in all elements of the array $ { the Dijkstra (Linkarr[i]); theminn=MAX; the for(intj=1; j<=want;++j)//Linkarr[i] The shortest possible destination in the the if(dis[wantarr[j]]<Minn) -minn=Dis[wantarr[j]]; in if(answer>Minn) theAnswer=Minn; the } About theprintf"%d\n", answer); the } the return 0; +}
The shortest path to a man's journey