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 5Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5 Time = 3: 3 -> 1 -> 5Sample 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 5Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5两次Dijkstra求最短路径和最快路径
如果最短路径不唯一,输出最短路径下的最快路径
如果最快路径不唯一,输出最快路径下节点数最少的
很常规的题目。额外判断最短和最快路径相同的情况,纪录path (前驱)
1087. All Roads Lead to Rome (30) <Dijkstra优先队列>
两个题目很类似
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
typedef pair<int,int> P;
const int INF=1<<29;
int n,m;
int le[555][555],tim[555][555];
int l[555],t[555];
int path_l[555]={0},path_t[555]={0};
int vi[555];
int cro[555];
vector<int> v[1000];
int x,y;
void Dijkstra_len(int num){
priority_queue<P,vector<P>,greater<P> > que;
for(int i=0;i<555;i++) l[i]=INF,vi[i]=0;
l[num]=0;
que.push({l[num],num});
while(que.size()){
P p=que.top();
que.pop();
int fz=p.second;
if(vi[fz]) continue;
vi[fz]=1;
for(int i=0;i<v[fz].size();i++){
int nu=v[fz][i];
if(l[nu]>l[fz]+le[fz][nu]){
l[nu]=l[fz]+le[fz][nu];
t[nu]=t[fz]+tim[fz][nu];
path_l[nu]=fz;
que.push({l[nu],nu});
}
else if(l[nu]==l[fz]+le[fz][nu]) {
if(t[nu]>t[fz]+tim[fz][nu]){
path_l[nu]=fz;
t[num]=t[fz]+tim[fz][nu];
}
}
}
}
}
void Dijkstra_time(int num){
priority_queue<P,vector<P>,greater<P> > que;
for(int i=0;i<555;i++) t[i]=INF,vi[i]=0,cro[i]=0;
t[num]=0;
cro[num]=1;
que.push({t[num],num});
while(que.size()){
P p=que.top();
que.pop();
int fz=p.second;
if(vi[fz]) continue;
vi[fz]=1;
for(int i=0;i<v[fz].size();i++){
int nu=v[fz][i];
if(t[nu]>t[fz]+tim[fz][nu]){
t[nu]=t[fz]+tim[fz][nu];
path_t[nu]=fz;
cro[nu]=cro[fz]+1;
que.push({t[nu],nu});
}
else if(t[nu]==t[fz]+tim[fz][nu]) {
if(cro[nu]>cro[fz]+1){
path_t[nu]=fz;
cro[nu]=cro[fz]+1;
}
}
}
}
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
le[i][j]=INF,tim[i][j]=INF;
}
for(int i=0;i<m;i++){
int a,b,c,d,e;
scanf("%d %d %d %d %d",&a,&b,&c,&d,&e);
v[a].push_back(b);
le[a][b]=d;
tim[a][b]=e;
if(c==0){
le[b][a]=d;
tim[b][a]=e;
v[b].push_back(a);
}
}
cin>>x>>y;
Dijkstra_len(x);
Dijkstra_time(x);
int Dis[1000],T[1000];
int cnt_d=0,cnt_t=0;
int fz=y;
stack<int> s;
while(fz!=x){
s.push(fz);
fz=path_l[fz];
}
while(s.size()){
Dis[cnt_d++]=s.top();
s.pop();
}
fz=y;
while(fz!=x){
s.push(fz);
fz=path_t[fz];
}
while(s.size()){
T[cnt_t++]=s.top();
s.pop();
}
if(cnt_t==cnt_d){
int i;
for( i=0;i<cnt_t;i++){
if(Dis[i]!=T[i]) break;
}
if(i==cnt_t){
printf("Distance = %d; Time = %d: %d",l[y],t[y],x);
for(int i=0;i<cnt_d;i++) printf(" -> %d",Dis[i]);
return 0;
}
}
printf("Distance = %d: %d",l[y],x);
for(int i=0;i<cnt_d;i++) printf(" -> %d",Dis[i]);
cout<<endl;
printf("Time = %d: %d",t[y],x);
for(int i=0;i<cnt_t;i++)
printf(" -> %d",T[i]);
return 0;
}