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
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
题意,给出出n图的顶点数,m图的边数,接下来是n行的边的数据,分别是,出发点,终点,是否有向,0无,1有,两点间的距离,两点间的时间,最后给出起点与终点,要求算出起点与终点间最短的路径,分2条,一条是最短的距离(距离相同时选择时间小的,题目保证路径唯一),第二条是起点与终点距离最小的(如果相同要求路径经过的点最少)
先输出最短距离的路径,输出起点到终点的最短距离还有路径, 在输出最短时间的路径,先输出最短时间,再输出路径,如果两条路径相同,则只输出一次路径,具体格式看上面输出
思路,用2次dijkstra,分别算出最短路,再在dijkstra的同时选择出最优的路径。
代码:
#include<bits/stdc++.h>
using namespace std;
int n;
const int N = 510;
const int INF = 0X3FFFFFFF;
int des[N][N];
int times[N][N];
int d[N];
int t[N];
int weight[N];
int pre[N];
bool vis[N];
int pt[N];
vector<int> path1,path2;
struct node {
int v,w;
node(int a, int b) {v = a,w = b;}
bool operator < (const node &b) const {
return w > b.w;
}
};
void dijkstra(int s) {
priority_queue<node> q;
fill(d, d + n, INF);
memset(weight, 0, sizeof(weight));
memset(vis, false, sizeof(vis));
weight[s] = 0;
d[s] = 0;
q.push(node(s, d[s]));
while(!q.empty()) {
node now = q.top();
q.pop();
int u = now.v;
if(vis[u]) continue;
vis[u] = true;
for(int v = 0; v < n; v++) {
if(des[u][v] != INF && vis[v] == false) {
if(d[v] > d[u] + des[u][v]) {
d[v] = d[u] + des[u][v];
weight[v] = weight[u] + times[u][v]; //更新该路径的时间
pre[v] = u;
q.push(node(v, d[v]));
} else if(d[v] == d[u] + des[u][v] && weight[v] > weight[u] + times[u][v]) {//最短路一样但是时间更优
// weight[v] = weight[u] + times[u][v];
pre[v] = u;
}
}
}
}
}
void dfs(int s, int dest) {
if(s == dest) {
path1.push_back(s);
return;
}
path1.push_back(s);
dfs(pre[s], dest);
}
void _dijkstra(int s) {
priority_queue<node> q;
fill(t, t + n, INF);
memset(weight, 0, sizeof(weight));
memset(vis, false, sizeof(vis));
pt[s] = 1;
t[s] = 0;
q.push(node(s, t[s]));
while(!q.empty()) {
node now = q.top();
q.pop();
int u = now.v;
if(vis[u]) continue;
vis[u] = true;
for(int v = 0; v < n; v++) {
if(vis[v] == false && times[u][v] != INF) {
if(t[v] > t[u] + times[u][v]) {
t[v] = t[u] + times[u][v];
pt[v] = pt[u] + 1;
pre[v] = u;
q.push(node(v, t[v]));
} else if(t[v] == t[u] + times[u][v] && pt[v] > pt[u] + 1) {
// pt[v] = pt[u]
pre[v] = u;
}
}
}
}
}
void _dfs(int s, int dest) {
if(s == dest) {
path2.push_back(s);
return;
}
path2.push_back(s);
_dfs(pre[s], dest);
}
bool judge(vector<int> a, vector<int> b) {
if(a.size() != b.size()) return false;
for(int i = 0; i < a.size(); i++) {
if(a[i] != b[i]) return false;
}
return true;
}
void show(vector<int> path) {
int _size = path.size();
for(int i = _size - 1; i >= 0; i--) {
printf("%d", path[i]);
if(i != 0) printf(" -> ");
else printf("\n");
}
}
int main() {
int m, s, dest;
scanf("%d %d", &n, &m);
fill(des[0], des[0] +N*N, INF);
fill(times[0], times[0] +N*N, INF);
for(int i = 0; i < m; i++) { //建图
int u, v, way, w, T;
scanf("%d %d %d %d %d", &u, &v, &way, &w, &T);
des[u][v] = w;
times[u][v] = T;
if(way == 0) {
des[v][u] = w;
times[v][u] = T;
}
}
//起点终点
scanf("%d %d", &s, &dest);
//2次dijkstra
//第一次算最短路&&时间尽可能小的路径
dijkstra(s);
dfs(dest, s); //计算路径;
memset(pre, 0, sizeof(pre));
_dijkstra(s);
_dfs(dest, s);
if(judge(path1, path2)) {
printf("Distance = %d; ", d[dest]);
} else if(path1 != path2) {
printf("Distance = %d: ", d[dest]);
show(path1);
}
printf("Time = %d: ", t[dest]);
show(path2);
return 0;
}