题目
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 ) N(2\le N\le500) N(2≤N≤500), and M M M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M M 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
N-1
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
题目大意
给出一些streets的端点v1,v2,如果是one-way的,即单程,那么只能从v1到v2,如果不是单程的,也可以从v2到v1,找出最短的路(不唯一,那就找出其中时间最短的)和时间最短的(不唯一,就找出经过地点最少的),如果说这两个路径一样,就只输出一次,按照题目给定的格式。
思路
dijkstra+dfs。
代码
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int INF = 0x7fffffff;
int n, m;
int s, d;
int lenmp[500][500], timemp[500][500];
int dist1[500], dist2[500];
bool visited[500];
vector<vector<int> > path(500);
int anstime = INF, anslen = INF, temp = 0;
vector<int> ans1, ans2, temppath;
void dijkstra1(){
fill(dist1, dist1+n, INF);
fill(visited, visited+n, false);
for(int i=0; i<n; i++){
if(lenmp[s][i] != -1){
dist1[i] = lenmp[s][i];
path[i].push_back(s);
}
}
visited[s] = true;
for(int i=1; i<n; i++){
int index, minx = INF;
for(int j=0; j<n; j++){
if(!visited[j] && dist1[j] < minx){
index = j;
minx = dist1[j];
}
}
visited[index] = true;
for(int j=0; j<n; j++){
if(!visited[j] && lenmp[index][j] != -1){
if(dist1[index] + lenmp[index][j] < dist1[j]){
dist1[j] = dist1[index] + lenmp[index][j];
path[j].clear();
path[j].push_back(index);
}
else if(dist1[index] + lenmp[index][j] == dist1[j])
path[j].push_back(index);
}
}
}
}
void dijkstra2(){
fill(dist2, dist2+n, INF);
fill(visited, visited+n, false);
for(int i=0; i<n; i++){
if(timemp[s][i] != -1){
dist2[i] = timemp[s][i];
path[i].push_back(s);
}
}
visited[s] = true;
for(int i=1; i<n; i++){
int index, minx = INF;
for(int j=0; j<n; j++){
if(!visited[j] && dist2[j] < minx){
index = j;
minx = dist2[j];
}
}
visited[index] = true;
for(int j=0; j<n; j++){
if(!visited[j] && timemp[index][j] != -1){
if(dist2[index] + timemp[index][j] < dist2[j]){
dist2[j] = dist2[index] + timemp[index][j];
path[j].clear();
path[j].push_back(index);
}
else if(dist2[index] + timemp[index][j] == dist2[j])
path[j].push_back(index);
}
}
}
}
void dfs1(int root){
if(root == s){
if(temp < anstime){
anstime = temp;
ans1 = temppath;
}
return;
}
for(int i=0; i<path[root].size(); i++){
temp += timemp[path[root][i]][root];
temppath.push_back(path[root][i]);
dfs1(path[root][i]);
temp -= timemp[path[root][i]][root];
temppath.pop_back();
}
}
void dfs2(int root){
if(root == s){
if(temppath.size() < anslen){
anslen = temppath.size();
ans2 = temppath;
}
return;
}
for(int i=0; i<path[root].size(); i++){
temppath.push_back(path[root][i]);
dfs2(path[root][i]);
temppath.pop_back();
}
}
bool check(){
if(ans1.size() != ans2.size())
return false;
for(int i=0; i<ans1.size(); i++)
if(ans1[i] != ans2[i])
return false;
return true;
}
int main(){
fill(lenmp[0], lenmp[0]+500*500, -1);
fill(timemp[0], timemp[0]+500*500, -1);
scanf("%d %d", &n, &m);
for(int i=0; i<m; i++){
int v1, v2, isone, len, time;
scanf("%d%d%d%d%d", &v1, &v2, &isone, &len, &time);
lenmp[v1][v2] = len, timemp[v1][v2] = time;
if(isone == 0)
lenmp[v2][v1] = len, timemp[v2][v1] = time;
}
scanf("%d%d", &s, &d);
temppath.push_back(d);
dijkstra1();
dfs1(d);
for(int i=0; i<n; i++)
path[i].clear();
dijkstra2();
dfs2(d);
if(check()){
printf("Distance = %d; Time = %d: ", dist1[d], dist2[d]);
printf("%d", ans1[ans1.size()-1]);
for(int i=ans1.size()-2; i>=0; i--)
printf(" -> %d", ans1[i]);
printf("\n");
}
else{
printf("Distance = %d: ", dist1[d]);
printf("%d", ans1[ans1.size()-1]);
for(int i=ans1.size()-2; i>=0; i--)
printf(" -> %d", ans1[i]);
printf("\n");
printf("Time = %d: ", dist2[d]);
printf("%d", ans2[ans2.size()-1]);
for(int i=ans2.size()-2; i>=0; i--)
printf(" -> %d", ans2[i]);
printf("\n");
}
return 0;
}