Solution: 1004 question Description: give you n vertices, 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 (t. 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 21 2 5 62 3 4 51 30 0 sample output: 9 11
# Include <iostream> # include <vector> using namespace std; typedef struct E {int next; int c; int cost;} E; bool mark [1001]; int dis [1001]; int cost [1001]; vector <E> edge [1001]; int main () {int N, M; while (cin> N> M) {if (N = 0 & M = 0) break; for (int I = 1; I <= N; I ++) {edge [I]. clear (); dis [I] =-1; mark [I] = false; cost [I] = 0;} while (M --) {int a, B, c, cost; cin> a> B> c> cost; E T; T. c = c; T. cost = cost; T. next = B; edge [a]. push_back (T); T. next = a; edge [B]. push_back (T);} int S, D; cin> S> D; dis [S] = 0; mark [S] = true; int newP = S; // start point is S, for (int I = 1; I <N; I ++) {for (int j = 0; j <edge [newP]. size (); j ++) {E tmp = edge [newP] [j]; int t = tmp. next; int c = tmp. c; int co = tmp. cost; if (mark [t] = true) continue; if (dis [t] =-1 | dis [t]> dis [newP] + c | (dis [t] = dis [newP] + c) & (cost [t]> cost [newP] + co) {dis [t] = dis [newP] + c; cost [t] = cost [newP] + co ;}} int min = 123123123; for (int j = 1; j <= N; j ++) {if (mark [j]) continue; if (dis [j] =-1) continue; if (dis [j] <min) {min = dis [j]; newP = j ;}} mark [newP] = true;} cout <dis [D] <"" <cost [D] <endl;} return 0 ;}