-
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 (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
Code:
# Include <iostream> using namespace STD; const int max = 65535; typedef struct graph {int Vex [1000]; int weight [1000] [1000]; // path length int cost [1000] [1000]; // takes int numvex, numedge;} graph; void create (graph * & G) // generates a graph {int I, w, C, J, K; CIN> G-> numvex> G-> numedge; for (I = 0; I <G-> numvex; I ++) for (j = 1; j <= G-> numvex; j ++) {G-> weight [I] [J] = max; g-> cost [I] [J] = max ;}for (k = 0; k <G-> numedge; k ++) {CIN> I> j> W> C; G-> weight [I] [J] = W; G-> cost [I] [J] = C; g-> weight [J] [I] = G-> weight [I] [J]; g-> cost [J] [I] = G-> cost [I] [J];} void mydijkstra (graph * & G, int start, int end) // Dijkstra algorithm {int mark1 [1000], mark2 [1000]; int dist1 [1000], dist2 [1000]; int I, j, K1, K2, min1, min2; for (I = 1; I <= G-> numvex; I ++) {dist1 [I] = max; dist2 [I] = max;} for (I = 1; I <= G-> numvex; I ++) {mark1 [I] = 0; mark2 [I] = 0; dist1 [I] = G-> W Eight [start] [I]; dist2 [I] = G-> cost [start] [I];} mark1 [start] = 1; mark2 [start] = 1; dist1 [start] = max; dist2 [start] = max; for (I = 1; I <G-> numvex; I ++) {min1 = max; min2 = max; j = 1; while (j <= G-> numvex) {If (! Mark1 [J] & dist1 [J] <min1) {min1 = dist1 [J]; k1 = J;} If (! Mark2 [J] & dist2 [J] <min2) {min2 = dist2 [J]; k2 = J;} J ++;} mark1 [k1] = 1; dist1 [k1] = min1; mark2 [k2] = 1; dist2 [k2] = min2; For (j = 1; j <= G-> numvex; j ++) {If (! Mark1 [J] & dist1 [J]> dist1 [k1] + G-> weight [k1] [J]) dist1 [J] = dist1 [k1] + G-> weight [k1] [J]; If (! Mark2 [J] & dist2 [J]> dist2 [k2] + G-> cost [k2] [J]) dist2 [J] = dist2 [k2] + G-> cost [k2] [J] ;}} cout <dist1 [end]; cout <""; cout <dist2 [end];} int main () {int start, end; graph * g = new graph; Create (g); CIN> Start> end; if (START = 0 & End = 0) return 0; mydijkstra (G, start, end); System ("pause"); Return 0 ;}