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
两点之间的时间也可以看作某种长度,再用dijkstra算法,并且注意第一标尺相同时比较第二标尺即可。
问题就是写的有点多了
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 510;
const int inf = 0x3fffffff;
int length[maxn][maxn], Time[maxn][maxn], n, m, suc, des;
int disL[maxn], disT[maxn], num[maxn], markL[maxn], markT[maxn], t[maxn];
int preL[maxn], preT[maxn];
void dijistra(int s) {
disT[s] = 0;
num[s] = 1;
for (int i = 0; i < n; i++) {
int pos = -1, min = inf;
for (int j = 0; j < n; j++) {
if (markT[j] == 0 && disT[j] < min) {
pos = j;
min = disT[j];
}
}
if (pos == -1)return;
markT[pos] = 1;
for (int j = 0; j < n; j++) {
if (markT[j] == 0) {
if (disT[j] > disT[pos] + Time[pos][j]) {
disT[j] = disT[pos] + Time[pos][j];
preT[j] = pos;
num[j] = num[pos] + 1;
}
else if (disT[j] == disT[pos] + Time[pos][j]) {
if (num[pos] + 1 < num[j]) {
preT[j] = pos;
num[j] = num[pos] + 1;
}
}
}
}
}
disL[s] = 0;
t[s] = 0;
for (int i = 0; i < n; i++) {
int pos = -1, min = inf;
for (int j = 0; j < n; j++) {
if (markL[j] == 0 && disL[j] < min) {
pos = j;
min = disL[j];
}
}
if (pos == -1)return;
markL[pos] = 1;
for (int j = 0; j < n; j++) {
if (markL[j] == 0) {
if (disL[j] > disL[pos] + length[pos][j]) {
disL[j] = disL[pos] + length[pos][j];
preL[j] = pos;
t[j]= t[j] = t[pos] + Time[pos][j];
}
else if (disL[j] == disL[pos] + length[pos][j]) {
if (t[pos] + Time[pos][j] < t[j]) {
preL[j] = pos;
t[j] = t[pos] + Time[pos][j];
}
}
}
}
}
}
void dfsL(int i) {
if (i == preL[i])return;
dfsL(preL[i]);
cout << " -> " << i;
}
void dfsT(int i) {
if (i == preT[i])return;
dfsT(preT[i]);
cout << " -> " << i;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
disT[i] = disL[i] = num[i] = t[i] = inf;
preL[i] = preT[i] = i;
markT[i] = markL[i] = 0;
for (int j = 0; j < n; j++) {
length[i][j] = Time[i][j] = inf;
}
}
for (int i = 0; i < m; i++) {
int x, y, dir, l, t;
cin >> x >> y >> dir >> l >> t;
if (dir == 0) {
length[y][x] = l;
Time[y][x] = t;
}
length[x][y] = l;
Time[x][y] = t;
}
cin >> suc >> des;
dijistra(suc);
int temp = des;
bool isSame = true;
while (temp != suc) {
if (preT[temp] != preL[temp]) {
isSame = false;
break;
}
temp = preL[temp];
}
if (isSame)cout << "Distance = " << disL[des] << "; ";
else {
cout << "Distance = " << disL[des] << ": " << suc;
dfsL(des);
cout << endl;
}
cout << "Time = " << disT[des] << ": " << suc;
dfsT(des);
cout << endl;
return 0;
}
本文介绍了一种算法,该算法能够找到从起点到终点的最短路径和最快路径。通过使用Dijkstra算法,我们可以在考虑距离和时间两种度量标准的情况下,为用户提供两个推荐路径。文章详细解释了如何处理街道的双向性和一维性,以及如何在存在多个最短或最快路径时选择最优解。
5360

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



