Problem address: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3790
The question shows that this question is the problem of finding the shortest path. | (* 'Port ')
This question is not difficult in algorithm, but requires attention in processing details. (? •?? •?)??
For example, when the input is: 1-> 2, the distance is 3, and the fee is 2.
2-> 1 distance is 1, cost is 5
Cost 5 for distance 1
1-> 2. The distance is 3. The fee is 2.
2-> 1, 3, and 5
The distance is 3, and the fee is 2.
This set of data comes from discuss. After reading it, I realized why wa. O (Clerk □clerk) O
Similarly, time cannot be processed separately:
Assume that s [T] = 11, time [T] = 5 appears in the operation; // The time to reach the destination is 11, and the time to reach the destination is 5
S [T] = 10, time [T] = 6;
In this case, the wrong program will take s [T] = 10, time [T] = 5.
Therefore, the distance should be prioritized based on the meaning of the question. If distance is updated in the calculation, the time must be updated;
If the distance is the same, consider whether the time needs to be updated.
I have adopted the dijstra algorithm here. It may not be well written. Sorry!
# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; const int maxn = 1000 + 10; const int INF = 65524*64-1; int map [maxn] [maxn]; int price [maxn] [maxn]; int path [maxn]; int cost [maxn]; bool vis [maxn]; int N, m, S, T; inline void input () {int I, j; for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) map [I] [J] = Price [I] [J] = inf; int A, B, D, P; for (I = 0; I <m; I ++) {scanf ("% d", & A, & B, & D, & P); // compare the distance first, and then compare the time if (MA P [a] [B]> d) {map [a] [B] = map [B] [a] = D; price [a] [B] = Price [B] [a] = P;} else if (Map [a] [B] = D) {If (price [a] [B]> P) price [a] [B] = P ;}} CIN >>> t; return ;} inline void dijstra () {int I; memset (VIS, false, sizeof (VIS); for (I = 1; I <= N; I ++) {path [I] = map [s] [I]; cost [I] = Price [s] [I];} vis [s] = true; path [s] = cost [s] = 0; int COUNT = 0; int minc, K; while (count <n-1) {k = 0; minc = inf; for (I = 1; I <= N; I ++) if (! Vis [I] & minc> path [I]) {minc = path [I]; k = I;} vis [k] = true; // ratio first, for (I = 1; I <= N; I ++) if (! Vis [I] & path [I]> path [k] + map [k] [I]) {path [I] = path [k] + map [k] [I]; cost [I] = cost [k] + price [k] [I];} else if (! Vis [I] & path [I] = path [k] + map [k] [I]) {If (cost [I]> cost [k] + price [k] [I]) cost [I] = cost [k] + price [k] [I];} count ++;} return;} int main () {While (CIN> N> M & (N | M) {input (); dijstra (); cout <path [T] <"" <cost [T] <Endl;} return 0 ;}
HDU 3790 shortest path problem