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
AC代码
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 501, INF = 1000000;
struct node{
int v, dis, time;
node(int a, int b, int c):v(a), dis(b), time(c){}
};
vector<node> Adj[maxn];
vector<int> path1, path2;
int d[maxn], t[maxn], num[maxn];
int pre_d[maxn], pre_t[maxn];
bool vis[maxn];
int n, m, s, e;
void Dijkstra_d(){
fill(vis, vis + maxn, false);
for(int i = 0; i < n; i++)
pre_d[i] = i;
fill(d, d + maxn, INF);
fill(t, t + maxn, INF);
d[s] = 0;
t[s] = 0;
for(int i = 0; i < n; i++){
int u = -1, min = INF;
for(int j = 0; j < n; j++){
if(d[j] < min && vis[j] == false){
min = d[j];
u = j;
}
}
if(u == -1) return;
vis[u] = true;
for(int j = 0; j < Adj[u].size(); j++){
int v = Adj[u][j].v;
if(vis[v] == false){
if(d[u] + Adj[u][j].dis < d[v]){
d[v] = d[u] + Adj[u][j].dis;
t[v] = t[u] + Adj[u][j].time;
pre_d[v]= u;
}
else if(d[u] + Adj[u][j].dis == d[v]
&& t[u] + Adj[u][j].time < t[v]){
t[v] = t[u] + Adj[u][j].time;
pre_d[v]= u;
}
}
}
}
}
void Dijkstra_t(){
fill(vis, vis + maxn, false);
for(int i = 0; i < n; i++)
pre_t[i] = i;
fill(t, t + maxn, INF);
fill(num, num + maxn, INF);
t[s] = 0;
num[s] = 1;
for(int i = 0; i < n; i++){
int u = -1, min = INF;
for(int j = 0; j < n; j++){
if(t[j] < min && vis[j] == false){
min = t[j];
u = j;
}
}
if(u == -1) return;
vis[u] = true;
for(int j = 0; j < Adj[u].size(); j++){
int v = Adj[u][j].v;
if(vis[v] == false){
if(t[u] + Adj[u][j].time < t[v]){
t[v] = t[u] + Adj[u][j].time;
num[v] = num[u] + 1;
pre_t[v]= u;
}
else if(t[u] + Adj[u][j].time == t[v]
&& num[u] + 1 < num[v]){
num[v] = num[u] + 1;
pre_t[v]= u;
}
}
}
}
}
void DFS_d(int v){
if(v == s){
path1.push_back(v);
return;
}
DFS_d(pre_d[v]);
path1.push_back(v);
}
void DFS_t(int v){
if(v == s){
path2.push_back(v);
return;
}
DFS_t(pre_t[v]);
path2.push_back(v);
}
int main(){
cin>>n>>m;
int u, v, one_way, dis, time;
for(int i = 0; i < m; i++){
cin>>u>>v>>one_way>>dis>>time;
Adj[u].push_back(node(v, dis, time));
if(one_way == 0)
Adj[v].push_back(node(u, dis, time));
}
cin>>s>>e;
Dijkstra_d();
Dijkstra_t();
DFS_d(e);
DFS_t(e);
if(path1 == path2){
cout<<"Distance = "<<d[e]<<"; Time = "<<t[e]<<": "<<path1[0];
for(int i = 1; i < path1.size(); i++)
cout<<" -> "<< path1[i];
}
else{
cout<<"Distance = "<<d[e]<<": "<<path1[0];
for(int i = 1; i < path1.size(); i++)
cout<<" -> "<< path1[i];
cout<<endl;
cout<<"Time = "<<t[e]<<": "<<path2[0];
for(int i = 1; i < path2.size(); i++)
cout<<" -> "<< path2[i];
}
return 0;
}

本文介绍了一种算法,该算法能够找到从起点到终点的最短路径和最快路径。通过使用Dijkstra算法的变体,它能够在考虑距离和时间的情况下推荐两条路径:一条是最短的,另一条是最快的。在输入地图的街道交点和街道信息后,算法能够处理任意请求,并确保至少存在一条路径。
593

被折叠的 条评论
为什么被折叠?



