Create a cost[ ] to record the cost
#include <iostream> #include <memory.h> #include <stdio.h> using namespace std; #define Max_V 1005 const int IN = (1<<28); int G[1005][1005]; int C[1005][1005]; int dist[1005]; int cost[1005]; int visited[1005]; int N,M,S,T; void dij() { for( int i = 1; i <= N; i++ ) { dist[i] = G[S][i]; cost[i] = C[S][i]; } dist[S] = 0; cost[S] = 0; visited[S] = 1; while(1) { int Min = IN, MinV = 0; for( int i = 1; i <= N; i++ ) { if( !visited[i] && dist[i] < Min ) { MinV = i; Min = dist[i]; } } if( !MinV ) break; else { visited[MinV] = 1; for( int i = 1; i <= N; i++ ) { if( !visited[i] && dist[i] > dist[MinV] + G[MinV][i] ) { dist[i] = dist[MinV] + G[MinV][i]; cost[i] = cost[MinV] + C[MinV][i]; } else if( !visited[i] && dist[i] == dist[MinV] + G[MinV][i] ) { if( cost[i] > cost[MinV] + C[MinV][i] ) { cost[i] = cost[MinV] + C[MinV][i]; } } } } } } intmain() { while( cin >> N >> M, N!=0 || M!=0) { memset(dist, 0, sizeof(dist)); memset(cost, 0, sizeof(cost)); memset(G, 0, sizeof(G)); memset(C, 0, sizeof(C)); memset(visited, 0, sizeof(visited)); for(int i = 1; i <= N; i++ ) for( int j = 1; j <= N; j++ ) { if( i==j ) { G[i][j] = 0; C[i][j] = 0; } else { G[i][j] = IN; C[i][j] = IN; } } while( M-- ) { int Begin,End,Length,Cost; scanf("%d %d %d %d", &Begin, &End, &Length, &Cost); if( Length < G[Begin][End] ) { G[Begin][End] = Length; G[End][Begin] = Length; C[End][Begin] = Cost; C[Begin][End] = Cost; } } cin >> S >> T; dij (); cout << dist[T] << ' ' << cost[T] << endl; } return 0; }