The shortest path.
The question is to give you a matrix, which is the cost of each point to each point. Then we will give you n numbers, which is the toll required for each point.
Then, ask a and B about the minimum fee and the path.
If it is not the path, the minimum Lexicographic Order must be output, which is very simple. Spfa write
It will be very troublesome. Then I will use Floyd. Update the path together.
# Include <cstdio> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <map> # include <stack> # include <iostream> # include <list> # include <set> # include <cmath> # define INF 0x7fffffff # define EPS 1e-6 # define ll long longusing namespace STD; int n, m; int G [101] [101]; int cost [101]; int path [101] [101]; void Floyd () {for (int K = 1; k <= N; k ++) {for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {If (G [I] [k] = inf | G [k] [J] = inf) continue; int Len = G [I] [k] + G [k] [J] + cost [k]; If (G [I] [J]> Len) {G [I] [J] = Len; path [I] [J] = path [I] [k];} else if (G [I] [J] = Len) {path [I] [J] = min (path [I] [J], path [I] [k]) ;}}} int main () {While (scanf ("% d", & N), n) {int TMP; for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {scanf ("% d ", & TMP); If (TMP =-1) g [I] [J] = inf; else G [I] [J] = TMP; path [I] [J] = J ;}for (INT I = 1; I <= N; I ++) scanf ("% D", & cost [I]); Floyd (); int I, j; while (scanf ("% d", & I, & J ), i! =-1 | j! =-1) {printf ("from % d to % d: \ n", I, j); printf ("Path: % d", I ); int K = I; while (K! = J) {printf ("--> % d", path [k] [J]); k = path [k] [J];} printf ("\ n"); printf ("Total cost: % d \ n", G [I] [J]) ;}}
HDU 1385 minimum transport cost