Problem Description
Give you n points, m undirected edges, each side has a length d and a cost p, give you a start point s end point t, it is required to output the shortest distance from the start point to the end point and its cost. If the shortest distance has multiple routes, the output will be the least costly.
Input
Enter n, m, and the vertex number is 1 ~ N, followed by m rows. Each row has four numbers a, B, d, p, indicating that there is an edge between a and B, and its length is d, and the cost is p. The last row contains two numbers (s), t; start point (s), and end point (s. When n and m are 0, the input ends.
(1 <n <= 1000, 0 <m <100000, s! = T)
Output
The output row has two numbers, the shortest distance and the cost.
Sample Input
3 2
1 2 5 6
2 3 4 5
1 3
0 0
Sample Output
9 11
# Include <stdio. h >#include <iostream >#include <queue> using namespace std; typedef struct n1 {int x, dist, mony; friend bool operator <(n1 a, n1 B) {if (B. dist>. dist) return B. dist <. dist; else if (B. dist =. dist & B. mony> =. mony) return B. mony <. mony ;}} node; node map [1005] [1005], N [1005]; int s, t, min_dist, min_mony; int vist [1005] [1005]; void set (int n, int m) {int I, j, n1, n2, d, p; for (I = 1; I <= n; I ++) {fo R (j = 1; j <= n; j ++) {map [I] [j]. dist = 0; vist [I] [j] = 0 ;}} while (m --) {scanf ("% d", & n1, & n2, & d, & p); if (map [n1] [n2]. dist = d) // when creating a map, pay attention to the problem of duplicate edge output during input. {if (map [n1] [n2]. mony> p) map [n1] [n2]. mony = map [n2] [n1]. mony = p;} else if (map [n1] [n2]. dist = 0 | map [n1] [n2]. dist> d) {map [n1] [n2]. dist = map [n2] [n1]. dist = d; map [n1] [n2]. mony = map [n2] [n1]. mony = p ;}} scanf ("% d", & s, & t);} void BFS (int n) {priority_queue <node> Q; Node q, p; int I; q. mony = 0; q. dist = 0; q. x = t; Q. push (q); while (! Q. empty () {q = Q. top (); Q. pop (); if (q. x = s) {min_dist = q. dist; min_mony = q. mony; break;} for (I = 1; I <= n; I ++) if (map [q. x] [I]. dist &&! Vist [q. x] [I]) {vist [q. x] [I] = vist [I] [q. x] = 1; // This will not repeat the path p. dist = map [q. x] [I]. dist + q. dist; p. mony = map [q. x] [I]. mony + q. mony; p. x = I; Q. push (p) ;}} int main () {int n, m; while (scanf ("% d", & n, & m)> 0 & (n | m) {set (n, m); BFS (n); printf ("% d \ n", min_dist, min_mony );}} /* 5 7 1 2 5 2 3 4 5 1 3 4 6 3 4 2 6 3 5 4 4 4 4 5 5 2 6 1 4 4 1 5 8 11 5 7 1 2 5 5 2 3 4 5 1 3 4 6 3 4 2 3 5 4 4 4 4 4 4 4 1 5 8 10 */# include <stdio. h >#include <iostream >#include <queue> using namespace std; typedef struct n1 {int x, dist, mony; friend bool operator <(n1 a, n1 B) {if (B. dist>. dist) return B. dist <. dist; else if (B. dist =. dist & B. mony> =. mony) return B. mony <. mony ;}} node; node map [1005] [1005], N [1005]; int s, t, min_dist, min_mony; int vist [1005] [1005]; void set (int n, int m) {int I, j, n1, n2, d, p; for (I = 1; I <= n; I ++) {( J = 1; j <= n; j ++) {map [I] [j]. dist = 0; vist [I] [j] = 0 ;}} while (m --) {scanf ("% d", & n1, & n2, & d, & p); if (map [n1] [n2]. dist = d) // when creating a map, pay attention to the problem of duplicate edge output during input. {if (map [n1] [n2]. mony> p) map [n1] [n2]. mony = map [n2] [n1]. mony = p;} else if (map [n1] [n2]. dist = 0 | map [n1] [n2]. dist> d) {map [n1] [n2]. dist = map [n2] [n1]. dist = d; map [n1] [n2]. mony = map [n2] [n1]. mony = p ;}} scanf ("% d", & s, & t);} void BFS (int n) {priority_queue <node> Q; node Q, p; int I; q. mony = 0; q. dist = 0; q. x = t; Q. push (q); while (! Q. empty () {q = Q. top (); Q. pop (); if (q. x = s) {min_dist = q. dist; min_mony = q. mony; break;} for (I = 1; I <= n; I ++) if (map [q. x] [I]. dist &&! Vist [q. x] [I]) {vist [q. x] [I] = vist [I] [q. x] = 1; // This will not repeat the path p. dist = map [q. x] [I]. dist + q. dist; p. mony = map [q. x] [I]. mony + q. mony; p. x = I; Q. push (p) ;}} int main () {int n, m; while (scanf ("% d", & n, & m)> 0 & (n | m) {set (n, m); BFS (n); printf ("% d \ n", min_dist, min_mony );}} /* 5 71 2 5 52 3 4 51 3 4 63 4 2 63 5 4 74 5 2 61 3 4 41 58 115 71 2 5 52 3 4 51 3 4 63 4 2 23 5 4 74 5 2 41 3 4 41 58 10 */