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 (2N500), 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 N1) 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 -> v1 -> ... -> destination

Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> ... -> 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 -> u1 -> ... -> 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 <stdio.h>
  2 #include <algorithm>
  3 #include <set>
  4 #include <string.h>
  5 #include <vector>
  6 #include <math.h>
  7 #include <queue>
  8 #include <iostream>
  9 #include <string>
 10 using namespace std;
 11 const int maxn = 530;
 12 const int inf = 99999999;
 13 int n,m;
 14 int vis[maxn];
 15 int d[maxn],c[maxn],pre[maxn],d2[maxn],c2[maxn];
 16 int g[maxn][maxn],co[maxn][maxn];
 17 void dijkstra(int st){
 18     fill(d,d+maxn,inf);
 19     fill(c,c+maxn,inf);
 20     fill(vis,vis+maxn,false);
 21     fill(pre,pre+maxn,-1);
 22     d[st]=0;
 23     c[st]=0;
 24     for(int i=0;i<n;i++){
 25         int u=-1,min=inf;
 26         for(int j=0;j<n;j++){
 27             if(vis[j]==false && d[j]<min){
 28                 min=d[j];
 29                 u=j;
 30             }
 31         }
 32         if(u==-1) return;
 33         vis[u]=true;
 34         for(int v=0;v<n;v++){
 35             if(vis[v]==false && g[u][v]!=inf){
 36                 if(d[v]>d[u]+g[u][v]){
 37                     d[v]=d[u]+g[u][v];
 38                     c[v]=c[u]+co[u][v];
 39                     pre[v]=u;
 40                 }
 41                 else if(d[v]==d[u]+g[u][v] && c[v]>c[u]+co[u][v]){
 42                     c[v]=c[u]+co[u][v];
 43                     pre[v]=u;
 44                 }
 45             }
 46         }
 47     }
 48 }
 49 void dijkstra2(int st){
 50     fill(d2,d2+maxn,inf);
 51     fill(c2,c2+maxn,inf);
 52     fill(vis,vis+maxn,false);
 53     fill(pre,pre+maxn,-1);
 54     d2[st]=0;
 55     c2[st]=0;
 56     for(int i=0;i<n;i++){
 57         int u=-1,min=inf;
 58         for(int j=0;j<n;j++){
 59             if(vis[j]==false && c2[j]<min){
 60                 min=c2[j];
 61                 u=j;
 62             }
 63         }
 64         if(u==-1) return;
 65         vis[u]=true;
 66         for(int v=0;v<n;v++){
 67             if(vis[v]==false && co[u][v]!=inf){
 68                 if(c2[v]>c2[u]+co[u][v]){
 69                     c2[v]=c2[u]+co[u][v];
 70                     d2[v]=d2[u]+1;
 71                     pre[v]=u;
 72                 }
 73                 else if(c2[v]==c2[u]+co[u][v] && d2[v]>d2[u]+1){
 74                     d2[v]=d2[u]+1;
 75                     pre[v]=u;
 76                 }
 77             }
 78         }
 79     }
 80 }
 81 vector<int> v1,v2;
 82 void dfs(vector<int> &v,int st,int ed){
 83     v.push_back(ed);
 84     if(st==ed)return;
 85     dfs(v,st,pre[ed]);
 86 }
 87 bool issame(vector<int> v1,vector<int> v2){
 88     if(v1.size()!=v2.size())return false;
 89     for(int i=0;i<v1.size()&&i<v2.size();i++){
 90         if(v1[i]!=v2[i]) return false;
 91     }
 92     return true;
 93 }
 94 int main(){
 95     scanf("%d %d",&n,&m);
 96     fill(g[0],g[0]+maxn*maxn,inf);
 97     fill(co[0],co[0]+maxn*maxn,inf);
 98     for(int i=0;i<m;i++){
 99         int v1,v2,way,len,time;
100         scanf("%d %d %d %d %d",&v1,&v2,&way,&len,&time);
101         g[v1][v2]=len;
102         co[v1][v2]=time;
103         if(way==0){
104             g[v2][v1]=len;
105             co[v2][v1]=time;
106         }
107     }
108     int st,ed;
109     scanf("%d %d",&st,&ed);
110     dijkstra(st);
111     dfs(v1,st,ed);
112     dijkstra2(st);
113     dfs(v2,st,ed);
114     if(issame(v1,v2)){
115         printf("Distance = %d; Time = %d: %d",d[ed],c2[ed],v1[v1.size()-1]);
116         for(int i=v1.size()-2;i>=0;i--){
117             printf(" -> %d",v1[i]);
118         }
119         return 0;
120     }
121     printf("Distance = %d: %d",d[ed],v1[v1.size()-1]);
122     for(int i=v1.size()-2;i>=0;i--){
123         printf(" -> %d",v1[i]);
124     }
125     printf("\n");
126     printf("Time = %d: %d",c2[ed],v2[v2.size()-1]);
127     for(int i=v2.size()-2;i>=0;i--){
128         printf(" -> %d",v2[i]);
129     }
130     printf("\n");
131 }
View Code

注意点:比较简单的最短路径题,就是算两个最小路径,都是有第二标尺的,再根据要求输出。又是题目没看清就开始做,导致一直答案错误,还有复制函数过来,总会落下一些地方没改,如果可以还是再敲一遍为好。