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 and are the indices (from 0 to V1
V2
N−1) of the two ends of the street; is 1 if the street is one-way from to , or 0 if not; is the length of the street; and is the time taken to pass the street.one-way
V1
V2
length
time
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 in the format:D
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
题意:给出每个节点的联通关系,输出两条路线:1、最短的路线,如果有多条最短的,则输出最快的。2、最快的路线,如果有多条,则输出最节点最少的路线。
思路:两次 dijkstra遍历,第一次选出最短的路线,第二次选出最快的路线;然后两次dfs,第一次选出最快的,第二次选出节点最少的路线。
#include <iostream>
#include "vector"
using namespace std;
const int maxx=510,INF=9999999;
int g[maxx][maxx],times[maxx][maxx],d[maxx],d1[maxx];
bool vis[maxx];
int n,m,s,des,shortfast=INF,fastfast=INF,mininter=INF;
vector<int> shortpath,pre[maxx],tempath,fastpath;
//选出最短的路线
void dfs(int st){
fill(d,d+maxx,INF);
d[st]=0;
for (int i = 0; i < n; ++i) {
int u=-1,MIN=INF;
for (int j = 0; j < n; ++j) {
if (!vis[j] && d[j] < MIN){
u=j;
MIN=d[j];
}
}
if (u==-1) return;
vis[u]= true;
for (int j = 0; j <n ; ++j) {
if (!vis[j] && g[u][j] < INF){
if (d[u] + g[u][j] < d[j]){
d[j] = d[u]+g[u][j];
pre[j].clear();
pre[j].push_back(u);
} else if (d[u]+g[u][j] == d[j]){
pre[j].push_back(u);
}
}
}
}
}
//选出耗时最短的
void dfs1(int st){
fill(d1,d1+maxx,INF);
d1[st]=0;
for (int i = 0; i < n; ++i) {
int u=-1,MIN=INF;
for (int j = 0; j < n; ++j) {
if (!vis[j] && d1[j] < MIN){
u=j;
MIN=d1[j];
}
}
if (u==-1) return;
vis[u]= true;
for (int j = 0; j <n ; ++j) {
if (!vis[j] && times[u][j] < INF){
if (d1[u] + times[u][j] < d1[j]){
d1[j] = d1[u]+times[u][j];
pre[j].clear();
pre[j].push_back(u);
} else if (d1[u]+times[u][j] == d1[j]){
pre[j].push_back(u);
}
}
}
}
}
//选出最短的路线中耗时最少的
void dfstravel(int v){
if (v==s){
tempath.push_back(v);
int cost=-1;
for (int i = tempath.size()-1; i > 0; --i) {
int now = tempath[i],next=tempath[i-1];
cost += times[now][next];
}
if (cost < shortfast){
shortfast = cost;
shortpath = tempath;
}
tempath.pop_back();
return;
}
tempath.push_back(v);
for (int i = 0; i < pre[v].size(); ++i) {
dfstravel(pre[v][i]);
}
tempath.pop_back();
}
//耗时最短的选出节点最少
void dfstravel1(int v){
if (v==s){
tempath.push_back(v);
int len=tempath.size();
// for (int i = tempath.size()-1; i > 0; --i) {
// int now = tempath[i],next=tempath[i-1];
// cost += times[now][next];
// }
if (len < mininter){
mininter = len;
fastpath = tempath;
}
tempath.pop_back();
return;
}
tempath.push_back(v);
for (int i = 0; i < pre[v].size(); ++i) {
dfstravel1(pre[v][i]);
}
tempath.pop_back();
}
int main() {
cin>>n>>m;
fill(g[0],g[0]+maxx*maxx,INF);
fill(times[0],times[0]+maxx*maxx,INF);
for (int i = 0; i < m; ++i) {
int v1,v2,flag,len,time;
cin>>v1>>v2>>flag>>len>>time;
g[v1][v2]=len;
times[v1][v2]=time;
if (flag==0){
g[v2][v1]=len;
times[v2][v1]=time;
}
}
cin>>s>>des;
dfs(s);
dfstravel(des);
fill(vis,vis+maxx, false);
dfs1(s);
dfstravel1(des);
if (shortpath != fastpath){
printf("Distance = %d: ",d[des]);
for (int i = shortpath.size()-1; i >=0 ; --i) {
if (i == shortpath.size()-1) printf("%d",shortpath[i]);
else printf(" -> %d",shortpath[i]);
}
printf("\n");
printf("Time = %d: ",d1[des]);
for (int i = fastpath.size()-1; i >=0 ; --i) {
if (i == fastpath.size()-1) printf("%d",fastpath[i]);
else printf(" -> %d",fastpath[i]);
}
} else {
printf("Distance = %d; Time = %d: ",d[des],d1[des]);
for (int i = fastpath.size()-1; i >=0 ; --i) {
if (i == fastpath.size()-1) printf("%d",fastpath[i]);
else printf(" -> %d",fastpath[i]);
}
}
}