Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2066
Test instructions Analysis: to the grass son home as the origin point, give the city between the time of arrival, give the grass son want to go to the city, the shortest time. Typical single source shortest circuit problem, directly with the Dijkstra can be, without optimization. It is important to note that the city is connected directly to the grass family, and the time can be counted as 0. In addition, the topic emphasizes that there can be multiple paths, so be careful to select one of the shortest.
/*a person's travel time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 20764 Accepted Submission (s): 7244Problem description Although grass son 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 the 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 data has more than one group, the first row of each group is three integers t,s and D, indicating that there is a T road, and the grass home adjacent to the city of S, where the grass wants to go to have D; then there is a T-line, each line has three integer a,b,time, indicating that the drive between a B city is Time hour; (1= < (b) <=1000;a,b may have multiple paths) followed by the number of S on line t+1, indicating that the city is connected to the grass family, and then the first line of T+2 has a D number, indicating that the grass wants to go to the place. Output outputs the shortest time a grass can go to a favorite city. Sample INPUT6 2 to 3 4 8 123 8 9 Output9 129 9 Authorgrass SOURCERPG session*///Dijkstra to find the shortest way#include <cstdio>#include<cstring>#defineINF 0XFFFFFConst intMAXN = ++ -;intV[MAXN], D[MAXN], W[MAXN][MAXN], T, S, D;intMin (intAintb) { returna > B?b:a;}voidinit () { for(inti =0; i < MAXN; i++) for(intj =0; J < Maxn; J + +) W[i][j]=INF; for(inti =0; i < MAXN; i++) D[i]= (i = =0?0: INF); memset (V,0,sizeof(v));}voidDijkstra () { for(inti =0; i < MAXN; i++){ intX, M =INF; for(inty =0; Y < MAXN; y++)if(!v[y] && d[y] <= m) m = d[x=y]; V[X]=1; for(inty =0; Y < MAXN; y++) D[y] = Min (D[y], d[x] +W[x][y]); }}intMain () {intU, VI, we; while(~SCANF ("%d%d%d", &t, &s, &D)) {init (); for(inti =0; I < T; i++) {scanf ("%d%d%d", &u, &vi, &we); W[U][VI]= W[vi][u] = W[u][vi] > we?We:w[u][vi]; } for(inti =0; I < S; i++) {scanf ("%d", &T); w[u][0] = w[0][u] =0; } Dijkstra (); intMin =INF; for(inti =0; i < D; i++) {scanf ("%d", &VI); if(D[vi] < min) min =D[vi];
} printf ("%d\n", Min); } return 0;}
HDU travel of 20,661 persons Dijkstra