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 -> 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
分析:Dijkstra模板题,套2次就行了。
/** * Copyright(c) * All rights reserved. * Author : Mered1th * Date : 2019-02-26-22.31.18 * Description : A1111 */ #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<string> #include<unordered_set> #include<map> #include<vector> #include<set> using namespace std; ; ; bool vis[maxn]; int G[maxn][maxn],d[maxn],t[maxn],T[maxn][maxn]; int n,m,st,ed; int pred[maxn],pret[maxn],mininter[maxn]; vector<int> shortPath,fastPath; void dDijkstra(int s){ fill(d,d+maxn,INF); fill(t,t+maxn,INF); d[s]=; t[s]=; memset(vis,,sizeof(vis)); ;i<n;i++) pred[i]=i; ;i<n;i++){ ,MIN=INF; ;j<n;j++){ if(vis[j]==false && d[j]<MIN){ u=j; MIN=d[j]; } } ) return; vis[u]=true; ;v<n;v++){ if(vis[v]==false && G[u][v]!=INF){ if(G[u][v]+d[u]<d[v]){ d[v]=d[u]+G[u][v]; t[v]=t[u]+T[u][v]; pred[v]=u; } else if(G[u][v]+d[u]==d[v]){ if(T[u][v]+t[u]<t[v]){ pred[v]=u; t[v]=T[u][v]+t[u]; } } } } } for(int i=ed;i!=st;i=pred[i]){ shortPath.push_back(i); } shortPath.push_back(st); } void tDijkstra(int s){ fill(t,t+maxn,INF); t[s]=; fill(mininter,mininter+maxn,); memset(vis,,sizeof(vis)); ;i<n;i++) pret[i]=i; ;i<n;i++){ ,MIN=INF; ;j<n;j++){ if(vis[j]==false && t[j]<MIN){ u=j; MIN=t[j]; } } ) return; vis[u]=true; ;v<n;v++){ if(vis[v]==false && T[u][v]!=INF){ if(T[u][v]+t[u]<t[v]){ t[v]=t[u]+T[u][v]; pret[v]=u; mininter[v]=mininter[u]+; } else if(T[u][v]+t[u]==t[v]){ ){ pret[v]=u; mininter[v]=mininter[u]+; } } } } } for(int i=ed;i!=st;i=pret[i]){ fastPath.push_back(i); } fastPath.push_back(st); } int main(){ #ifdef ONLINE_JUDGE #else freopen("1.txt", "r", stdin); #endif int u,v,o; cin>>n>>m; fill(G[],G[]+maxn*maxn,INF); fill(T[],T[]+maxn*maxn,INF); ;i<m;i++){ cin>>u>>v>>o; cin>>G[u][v]>>T[u][v]; ){ G[v][u]=G[u][v]; T[v][u]=T[u][v]; } } cin>>st>>ed; dDijkstra(st); tDijkstra(st); if(shortPath!=fastPath){ printf("Distance = %d: ",d[ed]); ;i>=;i--){ printf("%d",shortPath[i]); ) printf(" -> "); else printf("\n"); } printf("Time = %d: ",t[ed]); ;i>=;i--){ printf("%d",fastPath[i]); ) printf(" -> "); else printf("\n"); } } else{ printf("Distance = %d; Time = %d: ",d[ed],t[ed]); ;i>=;i--){ printf("%d",fastPath[i]); ) printf(" -> "); else printf("\n"); } } ; }