The journey of a man
Time
limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 20631 Accepted Submission (s): 7210
Problem description Although grass son is a road crazy (is in Hangzhou electric stay for more than a year, incredibly still will be lost in campus, sweat ~), but grass son still very much like travel, because in the journey will meet a lot of people (Prince Charming, ^0^), many things, but also enrich their experience, You can also 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 line 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, the grass wants to go where 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 paths between <=1000;a,b)
The next line of T+1 is the number of S, which indicates 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 outputs the shortest time a grass can go to a favorite city.
Sample Input
6 2 31 3 51 4 72 8 123 8 44 9 129 10 21 28 9 10
Sample Output
9
Resolution: Just to the grass home as the source point, the shortest path of the neighboring city is initially 0, and then use the Dijkstra algorithm processing, to find out the shortest distance of the grass home to all cities, and then found the grass want to go to the shortest path in the city of the minimum value. However, attention is no map!!!
AC Code:
#include <cstdio> #include <cstring> #include <algorithm>using namespace std; #define INF 1234567# Define MAXN + 2int V, E;int w[maxn][maxn];int VIS[MAXN], dis[maxn];void Dijkstra () {memset (Vis, 0, sizeof (VIS)); preprocessing for (int i=0; i<=v; i++) dis[i] = (i = = 0? 0:inf); for (int i=0; i<=v; i++) {//dijkstra int x, m = INF; for (int y=0; y<=v; y++)//Find the minimum value of unmarked d[y] if (!vis[y] && dis[y] <= m) {x = y; m = dis[x]; } Vis[x] = 1; for (int y=0; y<=v; y++) dis[y] = min (Dis[y], dis[x] + w[x][y]); }}int Main () {#ifdef sxk freopen ("In.txt", "R", stdin); #endif//sxk int T, S, E, time, S; while (scanf ("%d%d%d", &e, &s, &t)!=eof) {for (Int. i=0; i<maxn; i++) for (int j=0; J<MAXN ; J + +) W[i][j] = (i==j)? 0:inf; V = 0; for (int i=0; i<e; i++) {scanf ("%d%d%d", &s, &e, & time); v = max (max (V, s), E); W[s][e] = w[e][s] = min (w[s][e], time); } for (int i=0; i<s; i++) {scanf ("%d", &s); W[0][s] = w[s][0] = 0; } Dijkstra (); processing int ans = INF; Find the maximum jump height of int foo; for (int i=0; i<t; i++) {scanf ("%d", &foo); ans = min (ans, dis[foo]); } printf ("%d\n", ans); } return 0;}
HDU Travel of 20,661 persons (Dijkstra algorithm)