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 <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int maxn = 500 + 10;
const int inf = 0x3f3f3f3f;
int N, M, S, E;
int _dis[maxn][maxn], _time[maxn][maxn];
int dis[maxn], time_dis[maxn], disTime[maxn], cnt[maxn];
int vis[maxn];
int disPath[maxn], timePath[maxn];
vector<int> ans_dis_path, ans_time_path;
void distanceDij(){
memset(vis, 0, sizeof(vis));
for(int i = 0; i < N; ++i) dis[i] = (i == S ? 0 : inf);
for(int i = 0; i < N; ++i){
int x, m = inf;
for(int y = 0; y < N; ++y) if(!vis[y] && dis[y] <= m) m = dis[x=y];
vis[x] = 1;
for(int y = 0; y < N; ++y){
if(!vis[y] && _dis[x][y]){
if(dis[y] > dis[x] + _dis[x][y]){
dis[y] = dis[x] + _dis[x][y];
disPath[y] = x;
disTime[y] = disTime[x] + _time[x][y];
}else if(dis[y] == dis[x] + _dis[x][y] && disTime[y] > disTime[x] + _time[x][y]){
disTime[y] = disTime[x] + _time[x][y];
disPath[y] = x;
}
}
}
}
}
void timeDij(){
memset(vis, 0, sizeof(vis));
for(int i = 0; i < N; ++i) time_dis[i] = (i == S ? 0 : inf);
// vis[S] = 1;
for(int i = 0; i < N; ++i){
int x, m = inf;
for(int y = 0; y < N; ++y) if(!vis[y] && time_dis[y] <= m) m = time_dis[x=y];
vis[x] = 1;
for(int y = 0; y < N; ++y){
if(!vis[y] && _time[x][y]){
if(time_dis[y] > time_dis[x] + _time[x][y]){
time_dis[y] = time_dis[x] + _time[x][y];
timePath[y] = x;
cnt[y] = cnt[x] + 1;
}else if(time_dis[y] == time_dis[x] + _time[x][y] && cnt[y] > cnt[x] + 1){
timePath[y] = x;
cnt[y] = cnt[x] + 1;
}
}
}
}
}
void getDisPath(){
int tmp = E;
while(tmp != S){
ans_dis_path.push_back(tmp);
tmp = disPath[tmp];
}
ans_dis_path.push_back(S);
}
void getTimePath(){
int tmp = E;
while(tmp != S){
ans_time_path.push_back(tmp);
tmp = timePath[tmp];
}
ans_time_path.push_back(S);
}
int main(){
scanf("%d %d", &N, &M);
for(int i = 0; i < M; ++i){
int u, v, o, l, t;
scanf("%d %d %d %d %d", &u, &v, &o, &l, &t);
_dis[u][v] = l, _time[u][v] = t;
if(!o) _dis[v][u] = l, _time[v][u] = t;
}
scanf("%d %d", &S, &E);
distanceDij();
timeDij();
getDisPath();
getTimePath();
if(ans_dis_path == ans_time_path){
printf("Distance = %d; Time = %d: ", dis[E], time_dis[E]);
int i = 0;
for(auto it = ans_dis_path.rbegin(); it != ans_dis_path.rend(); ++it){
if(i++) printf(" -> ");
printf("%d", *it);
}
}else{
printf("Distance = %d: ", dis[E]);
int i = 0;
for(auto it = ans_dis_path.rbegin(); it != ans_dis_path.rend(); ++it){
if(i++) printf(" -> ");
printf("%d", *it);
}
printf("\nTime = %d: ", time_dis[E]);
i = 0;
for(auto it = ans_time_path.rbegin(); it != ans_time_path.rend(); ++it){
if(i++) printf(" -> ");
printf("%d", *it);
}
}
return 0;
}