Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:
V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to the destination with distance D in the format:
Distance = D: source -> v~1~ -> ... -> destination
Then in the next line print the fastest path with total time T:
Time = T: source -> w~1~ -> ... -> destination
In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print them in one line in the format:
Distance = D; Time = T: source -> u~1~ -> ... -> destination
Sample Input 1:
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5
Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5
Sample Input 2:
7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5
Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 using namespace std; 5 const int inf = 99999999; 6 vector<vector<int> > G(500, vector<int>(500, 0)), length(500, vector<int>(500, inf)), time1(500, vector<int>(500, inf)); 7 vector<int> vis(500), tempPath, shortestPath, fastestPath; 8 vector<int> mindis(500, inf), shortPre[500], mintime(500, inf), fastPre[500]; 9 int n , m, s, e; 10 int minTime=inf, minSec=inf; 11 12 void dfs1(int v, int t){ 13 tempPath.push_back(v); 14 if(v==s){ 15 if(minTime>t){ 16 minTime = t; 17 shortestPath=tempPath; 18 } 19 tempPath.pop_back(); 20 return; 21 } 22 for(int i=0; i<shortPre[v].size(); i++) dfs1(shortPre[v][i], t+time1[v][shortPre[v][i]]); 23 tempPath.pop_back(); 24 } 25 26 void dfs2(int v, int cnt){ 27 tempPath.push_back(v); 28 if(v==s){ 29 if(cnt<minSec){ 30 minSec=cnt; 31 fastestPath=tempPath; 32 } 33 tempPath.pop_back(); 34 return; 35 } 36 for(int i=0; i<fastPre[v].size(); i++) dfs2(fastPre[v][i], cnt+1); 37 tempPath.pop_back(); 38 } 39 int main(){ 40 int i; 41 scanf("%d%d", &n, &m); 42 for(i=0; i<m; i++){ 43 int v1, v2, oneWay, Length, Time; 44 scanf("%d%d%d%d%d", &v1, &v2, &oneWay, &Length, &Time); 45 if(oneWay) G[v1][v2] = 1; 46 else G[v1][v2] = G[v2][v1] = 1; 47 length[v1][v2] = length[v2][v1] = Length; 48 time1[v2][v1] = time1[v1][v2] = Time; 49 } 50 scanf("%d%d", &s, &e); 51 int minn, u, j, v; 52 fill(vis.begin(), vis.end(), false); 53 mindis[s]=0; 54 for(i=0; i<n; i++){ 55 minn = inf; u = -1; 56 for(j=0; j<n; j++){ 57 if(!vis[j] && mindis[j]<minn){ 58 minn = mindis[j]; 59 u = j; 60 } 61 } 62 if(u==-1) break; 63 vis[u] = true; 64 for(v=0; v<n; v++){ 65 if(!vis[v] && G[u][v]){ 66 if(mindis[v] > mindis[u]+length[u][v]){ 67 shortPre[v].clear(); 68 shortPre[v].push_back(u); 69 mindis[v] = mindis[u] + length[u][v]; 70 }else if(mindis[v] == mindis[u]+length[u][v]){ 71 shortPre[v].push_back(u); 72 } 73 } 74 } 75 } 76 dfs1(e, 0); 77 fill(vis.begin(), vis.end(), false); 78 mintime[s]=0; 79 for(i=0; i<n; i++){ 80 minn=inf; u=-1; 81 for(j=0; j<n; j++){ 82 if(!vis[j] && mintime[j]<minn){ 83 minn = mintime[j]; 84 u=j; 85 } 86 } 87 if(u==-1) break; 88 vis[u]=true; 89 for(v=0; v<n; v++){ 90 if(!vis[v] && G[u][v]){ 91 if(mintime[v]>mintime[u]+time1[u][v]){ 92 fastPre[v].clear(); 93 fastPre[v].push_back(u); 94 mintime[v]=mintime[u]+time1[u][v]; 95 }else if(mintime[v]==mintime[u]+time1[u][v]){ 96 fastPre[v].push_back(u); 97 } 98 } 99 } 100 } 101 dfs2(e, 0); 102 if(shortestPath==fastestPath){ 103 printf("Distance = %d; Time = %d: ", mindis[e], mintime[e]); 104 for(i=shortestPath.size()-1; i>=0; i--){ 105 printf("%d", shortestPath[i]); 106 if(i!=0) printf(" -> "); 107 } 108 }else{ 109 printf("Distance = %d: ", mindis[e]); 110 for(i=shortestPath.size()-1; i>=0; i--){ 111 printf("%d", shortestPath[i]); 112 if(i!=0) printf(" -> "); 113 } 114 printf(" "); 115 printf("Time = %d: ", mintime[e]); 116 for(i=fastestPath.size()-1; i>=0; i--){ 117 printf("%d", fastestPath[i]); 118 if(i!=0) printf(" -> "); 119 } 120 121 } 122 return 0; 123 }