1111 Online Map (30 分)
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
分析:需要两条标准不同的路径,第一条路径需要最短距离,第二标尺为时间;第二条路径需要最短时间,第二标尺为路径中节点数。分别用两次Dijkstra + DFS来搜索符合要求的路径。第一次使用后需要重新清零visit数组。搜索结束后判断两条路径是否相同。
代码:
#include<iostream>
#include<algorithm>
#include<vector>
#define MAXN 1002
#define INF 0x3fffffff
using namespace std;
int N, M;
int S, D;
int G[MAXN][MAXN];
int d[MAXN];
int C[MAXN][MAXN];
int c[MAXN];
int visit[MAXN] = { 0 };
vector<int> preT[MAXN], preD[MAXN];
vector<int> pathT, tempPathT;
vector<int> pathD, tempPathD;
int minDis = INF, minTime = INF;
int minDisT = INF;
bool isSame = true;
void dfsD(int v) {
tempPathD.push_back(v);
if (v == S) {
int dis = 0;
int disT = 0;
for (int i = 0; i < tempPathD.size() - 1; i++) {
int index = tempPathD[i];
int index_1 = tempPathD[i + 1];
dis += G[index_1][index];
disT += C[index_1][index];
}
if (dis < minDis) {
minDis = dis;
minDisT = disT;
pathD = tempPathD;
} else if (dis == minDis) {
if (disT < minDisT) {
minDisT = disT;
pathD = tempPathD;
}
}
}
for (int i = 0; i < preD[v].size(); i++) {
dfsD(preD[v][i]);
}
tempPathD.pop_back();
}
void dfsT(int v) {
tempPathT.push_back(v);
if (v == S) {
int time = 0;
for (int i = 0; i < tempPathT.size() - 1; i++) {
int index = tempPathT[i];
int index_1 = tempPathT[i + 1];
time += C[index_1][index];
}
if (time < minTime) {
minTime = time;
pathT = tempPathT;
} else if (time == minTime) {
if (tempPathT.size() < pathT.size()) {
pathT = tempPathT;
}
}
}
for (int i = 0; i < preT[v].size(); i++) {
dfsT(preT[v][i]);
}
tempPathT.pop_back();
}
void dijkstraD() {
d[S] = 0;
for (int i = 0; i < N; i++) {
int u = -1, minN = INF;
for (int j = 0; j < N; j++) {
if (d[j] < minN && visit[j] == 0) {
u = j;
minN = d[j];
}
}
if (u == -1) {
return;
}
visit[u] = 1;
for (int v = 0; v < N; v++) {
if (G[u][v] != INF && visit[v] == 0) {
if (d[v] > d[u] + G[u][v]) {
d[v] = d[u] + G[u][v];
preD[v].clear();
preD[v].push_back(u);
} else if (d[v] == d[u] + G[u][v]) {
preD[v].push_back(u);
}
}
}
}
}
void dijkstraT() {
c[S] = 0;
for (int i = 0; i < N; i++) {
int u = -1, minN = INF;
for (int j = 0; j < N; j++) {
if (c[j] < minN && visit[j] == 0) {
u = j;
minN = c[j];
}
}
if (u == -1) {
return;
}
visit[u] = 1;
for (int v = 0; v < N; v++) {
if (C[u][v] != INF && visit[v] == 0) {
if (c[v] > c[u] + C[u][v]) {
c[v] = c[u] + C[u][v];
preT[v].clear();
preT[v].push_back(u);
} else if (c[v] == c[u] + C[u][v]) {
preT[v].push_back(u);
}
}
}
}
}
int main() {
fill(G[0], G[0] + MAXN * MAXN, INF);
fill(C[0], C[0] + MAXN * MAXN, INF);
fill(d, d + MAXN, INF);
fill(c, c + MAXN, INF);
cin >> N >> M;
for (int i = 0; i < M; i++) {
int V1, V2, one_way, length, time;
cin >> V1 >> V2 >> one_way >> length >> time;
if (one_way == 1) {
G[V1][V2] = length;
C[V1][V2] = time;
} else {
G[V1][V2] = G[V2][V1] = length;
C[V1][V2] = C[V2][V1] = time;
}
}
cin >> S >> D;
dijkstraD();
dfsD(D);
fill(visit, visit + MAXN, 0);
pathT.resize(MAXN);
dijkstraT();
dfsT(D);
int len = min(pathD.size(), pathT.size());
for (int i = 0; i < len; i++) {
if (pathD[i] != pathT[i]) {
isSame = false;
break;
}
}
if (isSame == true) {
cout << "Distance = " << minDis << "; Time = " << minTime << ": ";
for (int i = pathD.size() - 1; i >= 0; i--) {
if (i != 0) {
cout << pathD[i] << " -> ";
} else {
cout << pathD[i] << endl;
}
}
} else {
cout << "Distance = " << minDis << ": ";
for (int i = pathD.size() - 1; i >= 0; i--) {
if (i != 0) {
cout << pathD[i] << " -> ";
} else {
cout << pathD[i] << endl;
}
}
cout << "Time = " << minTime << ": ";
for (int i = pathT.size() - 1; i >= 0; i--) {
if (i != 0) {
cout << pathT[i] << " -> ";
} else {
cout << pathT[i] << endl;
}
}
}
return 0;
}