Travel by one person
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 18044 accepted submission (s): 6266
Problem description although caoer is a luchi (that is, the person who has been in Hangzhou for more than a year will still be lost on campus, Khan ~), However, caoer still enjoys traveling, because he will meet many people (Prince Charming, ^ 0 ^) on the road. Many things can enrich his experience, you can also see beautiful scenery ...... Caoer wants to go to many places. She wants to go to the Tokyo Tower to see the night view, go to Venice to see the movie, go to Yangmingshan to see the taro, go to New York to see the pure snow scene, go to Paris to drink coffee and write, visit Meng jiangnv in Beijing ...... The winter vacation is approaching. You can't waste it for such a long period of time. You must give yourself a good vacation, but you can't waste your training, therefore, caoer decided to go to a desired place in the shortest time! Because caoer's home is in a small town without passing by train, she can only take a train to the neighboring city ~).
There are multiple groups of input data. The first line of each group is three integers t, s, and D, indicating that there are t routes and S are adjacent to cao'er's city, there are d places to go;
Then there are t rows. Each row has three integers A, B, and time, indicating that the driving distance between cities A and B is time hour. (1 = <(A, B) <= 1000; there may be multiple routes between A and B)
The next line t + 1 contains the number of S, indicating the city connected to cao'er's home;
The next line T + 2 has the number D, which indicates that the grass wants to go to the place.
Output outputs the shortest time for 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
This is also the most short-circuit problem. I think the starting point of this question is the home of caoer. The time spent between her home and the neighboring city is 0, which will not affect the final result, this also avoids multiple loops and causes timeout.
Code:
# Include <stdio. h>
# Include <string. h>
# Define INF 1 <30
Int A = 0, B = 0, time = 0;
Int dis [1001]; // used to record time and update
Int map [1001] [1001]; // used to record the distance between two points
Int C [1001]; // used to record the end point
Int max = 0; // indicates the maximum number of records.
Int used [1001]; // used to mark
// Input function, coexistence Diagram
Void input (int t, int S, int D)
{
Int I = 0, j = 0;
For (I = 0; I <1001; I ++)
{
For (j = 0; j <1001; j ++)
{
Map [I] [J] = inf;
}
Dis [I] = inf;
Used [I] = 0;
}
For (I = 0; I <t; I ++)
{
Scanf ("% d", & A, & B, & time );
If (Map [a] [B]> time)
{
Map [a] [B] = time;
Map [B] [a] = time;
}
Max = max>? MAX:;
Max = max> B? MAX: B;
}
For (I = 0; I <s; I ++)
{
Int x = 0;
Scanf ("% d", & X );
Map [0] [x] = 0;
}
For (I = 0; I <D; I ++)
Scanf ("% d", & C [I]);
}
Void dijstra (int n)
{
Int I = 0, j = 0;
Bool R;
Int C = 0;
For (I = 0; I <= N; I ++)
{
R = false;
Int min = inf;
// Find the point that takes the shortest time
For (j = 0; j <= N; j ++)
{
If (! Used [J] & min> dis [J])
{
R = true;
Min = dis [J];
C = J;
}
}
// If the vertex is not found, the shortest path of all vertices is completed.
If (! R)
Return;
// Otherwise, mark the vertex
Used [c] = 1;
// Relaxation operation
For (j = 0; j <= N; j ++)
{
If (! Used [J] & dis [J]> dis [c] + map [C] [J])
Dis [J] = dis [c] + map [C] [J];
}
}
}
Int main ()
{
Int T, S, D;
While (~ Scanf ("% d", & T, & S, & D ))
{
If (D = 0)
Printf ("0 \ n ");
Int I = 0, j = 0;
Max = 0;
Int M = inf;
Input (t, s, d );
For (I = 0; I <D; I ++)
{
Dis [0] = 0;
Dijstra (max );
If (M> dis [C [I])
M = dis [C [I];
}
Printf ("% d \ n", M );
Memset (MAP, 0, sizeof (MAP ));
Memset (C, 0, sizeof (c ));
}
Return 0;
}