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
#include <stdio.h>
#include <algorithm>
#include <set>
#include <string.h>
#include <vector>
#include <math.h>
#include <queue>
#include <iostream>
#include <string>
using namespace std;
const int maxn = ;
const int inf = ;
int n,m;
int vis[maxn];
int d[maxn],c[maxn],pre[maxn],d2[maxn],c2[maxn];
int g[maxn][maxn],co[maxn][maxn];
void dijkstra(int st){
fill(d,d+maxn,inf);
fill(c,c+maxn,inf);
fill(vis,vis+maxn,false);
fill(pre,pre+maxn,-);
d[st]=;
c[st]=;
for(int i=;i<n;i++){
int u=-,min=inf;
for(int j=;j<n;j++){
if(vis[j]==false && d[j]<min){
min=d[j];
u=j;
}
}
if(u==-) return;
vis[u]=true;
for(int v=;v<n;v++){
if(vis[v]==false && g[u][v]!=inf){
if(d[v]>d[u]+g[u][v]){
d[v]=d[u]+g[u][v];
c[v]=c[u]+co[u][v];
pre[v]=u;
}
else if(d[v]==d[u]+g[u][v] && c[v]>c[u]+co[u][v]){
c[v]=c[u]+co[u][v];
pre[v]=u;
}
}
}
}
}
void dijkstra2(int st){
fill(d2,d2+maxn,inf);
fill(c2,c2+maxn,inf);
fill(vis,vis+maxn,false);
fill(pre,pre+maxn,-);
d2[st]=;
c2[st]=;
for(int i=;i<n;i++){
int u=-,min=inf;
for(int j=;j<n;j++){
if(vis[j]==false && c2[j]<min){
min=c2[j];
u=j;
}
}
if(u==-) return;
vis[u]=true;
for(int v=;v<n;v++){
if(vis[v]==false && co[u][v]!=inf){
if(c2[v]>c2[u]+co[u][v]){
c2[v]=c2[u]+co[u][v];
d2[v]=d2[u]+;
pre[v]=u;
}
else if(c2[v]==c2[u]+co[u][v] && d2[v]>d2[u]+){
d2[v]=d2[u]+;
pre[v]=u;
}
}
}
}
}
vector<int> v1,v2;
void dfs(vector<int> &v,int st,int ed){
v.push_back(ed);
if(st==ed)return;
dfs(v,st,pre[ed]);
}
bool issame(vector<int> v1,vector<int> v2){
if(v1.size()!=v2.size())return false;
for(int i=;i<v1.size()&&i<v2.size();i++){
if(v1[i]!=v2[i]) return false;
}
return true;
}
int main(){
scanf("%d %d",&n,&m);
fill(g[],g[]+maxn*maxn,inf);
fill(co[],co[]+maxn*maxn,inf);
for(int i=;i<m;i++){
int v1,v2,way,len,time;
scanf("%d %d %d %d %d",&v1,&v2,&way,&len,&time);
g[v1][v2]=len;
co[v1][v2]=time;
if(way==){
g[v2][v1]=len;
co[v2][v1]=time;
}
}
int st,ed;
scanf("%d %d",&st,&ed);
dijkstra(st);
dfs(v1,st,ed);
dijkstra2(st);
dfs(v2,st,ed);
if(issame(v1,v2)){
printf("Distance = %d; Time = %d: %d",d[ed],c2[ed],v1[v1.size()-]);
for(int i=v1.size()-;i>=;i--){
printf(" -> %d",v1[i]);
}
return ;
}
printf("Distance = %d: %d",d[ed],v1[v1.size()-]);
for(int i=v1.size()-;i>=;i--){
printf(" -> %d",v1[i]);
}
printf("\n");
printf("Time = %d: %d",c2[ed],v2[v2.size()-]);
for(int i=v2.size()-;i>=;i--){
printf(" -> %d",v2[i]);
}
printf("\n");
}
注意点:比较简单的最短路径题,就是算两个最小路径,都是有第二标尺的,再根据要求输出。又是题目没看清就开始做,导致一直答案错误,还有复制函数过来,总会落下一些地方没改,如果可以还是再敲一遍为好。