Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2680
Choose the best route
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 7201 accepted submission (s): 2350
Problem descriptionone day, Kiki wants to visit one of her friends. as she is liable to carsickness, she wants to arrive at her friend's home as soon as possible. now give you a map of the city's traffic route, and the stations which are near Kiki's home so that she can take. you may suppose Kiki can change the bus at any station. please find out the least time Kiki needs to spend. to make it ea Sy, if the city have n bus stations, the stations will been expressed as an integer 1, 2, 3... N.
Inputthere are several test cases.
Each case begins with three integers n, m and S, (n <1000, m <20000,1 = <S <= N) N stands for the number of bus stations in this city and M stands for the number of directed ways between bus stations. (maybe there are several ways between two bus stations .) S stands for the bus station that near Kiki's friend's home.
Then follow M lines, each line contains three integers p, q, T (0 <t <= 1000 ). means from station P to station Q there is a way and it will costs t minutes.
Then a line with an integer W (0 <W <n), means the number of stations Kiki can take at the beginning. Then follows W integers stands for these stations.
Outputthe output contains one line for each data set: the least time Kiki needs to spend, if it's impossible to find such a route, just output "-1 ".
Sample input5 8 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211
Sample Output1-1 questions: This question of the variable is more, so it is more disgusting. But that is, the Dijkstra template problem ~ It also gives the start point and end point and finds the shortest path. Here is a tip: Set the virtual starting point to "0", set the distance from 0 to any real starting point to 0, and the distance from 0 to other points to infinity, which can save a lot of time !! Another thing to note is that this path is
UnidirectionalToday, I have just passed level 4 English. I think it's okay to look at the English questions ~ (* ^__ ^ *) Xi ......
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 int node[1010],map[1010][1010],n,Min; 6 const int INF=9999999; 7 8 void dijkstra() 9 {10 int vis[1010]= {0};11 int tm=0,m;12 node[tm]=0;13 vis[tm]=1;14 for(int i = 1; i <= n; i++)15 node[i] = INF;16 for (int k=2; k<=n; k++)17 {18 Min=INF;19 for (int i=1; i<=n; i++)20 if (!vis[i])21 {22 if (node[i]>map[tm][i]+node[tm])23 node[i]=map[tm][i]+node[tm];24 if (Min>node[i])25 {26 Min=node[i];27 m=i;28 }29 }30 vis[m]=1;31 tm=m;32 }33 }34 35 int main ()36 {37 int m,s;38 while (~scanf("%d%d%d",&n,&m,&s))39 {40 memset(map, INF, sizeof(map));41 for (int i=1; i<=m; i++)42 {43 int p,q,t;44 cin>>p>>q>>t;45 if (map[p][q]>t)46 map[p][q]=t;47 }48 int w,cost;49 cin>>w;50 for (int i=1; i<=w; i++)51 {52 scanf ("%d",&cost);53 map[0][cost]=0;54 }55 dijkstra();56 if(node[s] ==INF)57 printf("-1\n");58 else59 printf("%d\n", node[s]);60 }61 return 0;62 }