Travel by one person
Time Limit: 1000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 52212 accepted submission (s): 17369
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 input6 2 31 3 51 4 72 8 123 8 44 9 129 10 21 28 9 10
Sample output9
The bus journey between two cities is provided. And adjacent cities, and target cities. Find the shortest journey from the neighboring city to the target city.
Question: because the number of cities in this question can reach 1000 at most, we use the Floyd algorithm to compare the limit, so we need to optimize it !! Otherwise, t will be very happy. The minimum driving distance from each adjacent city to the target city can be obtained in the Floyd algorithm, and the min function is not used. In the Floyd algorithm, If I to K does not have a car, skip directly to the next one, so you can reduce the time of a for loop.
1 # include <bits/stdc ++. h> 2 using namespace STD; 3 int T, S, D; 4 int A [1005] [1005]; 5 Int B [1005], C [1005]; 6 const int INF = 0x3f3f3f; 7 int n = 0; 8 int Floyd () {9 int Minn = inf; 10 for (int K = 1; k <= N; k ++) {11 for (INT I = 1; I <= N; I ++) {12 if (a [I] [k]! = Inf) {// The optimization determines if there is no vehicle between the two cities and the next one continues. The key is 13 for (Int J = 1; j <= N; j ++) {14 // A [I] [J] = min (A [I] [J], a [I] [k] + A [k] [J]); 15 A [I] [J] = A [I] [J] <A [I] [k] + A [k] [J]? A [I] [J]: A [I] [k] + A [k] [J]; 16 if (B [I] & C [J] & Minn> A [I] [J]) 17 Minn = A [I] [J]; 18} 19} 20 21} 22} 23 return Minn; 24} 25 void Init () {26 for (INT I = 0; I <1005; I ++) {27 for (Int J = 0; j <1005; j ++) {28 A [I] [J] = inf; 29} 30 A [I] [I] = 0; 31} 32} 33 int main () {34 while (~ Scanf ("% d", & T, & S, & D) {35 Init (); 36 memset (B, 0, sizeof (B )); 37 memset (C, 0, sizeof (c); 38 for (INT I = 1; I <= T; I ++) {39 int x, y, z; 40 scanf ("% d", & X, & Y, & Z); 41 if (z <A [x] [Y]) A [x] [Y] = A [y] [x] = z; 42 if (n <X) n = x; 43 If (n <Y) n = y; 44} 45 for (INT I = 0; I <s; I ++) {46 int temp; 47 scanf ("% d", & temp ); 48 B [temp] = 1; 49} 50 for (INT I = 0; I <D; I ++) {51 int temp; 52 scanf ("% d ", & temp); 53 c [temp] = 1; 54} 55 printf ("% d \ n", Floyd (); 56 n = 0; 57} 58 return 0; 59}
Hdu2066 one-person travel (Floyd optimized)