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.
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.
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
- 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
- Distance = 6: 3 -> 4 -> 8 -> 5
- Time = 3: 3 -> 1 -> 5
- 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
Distance = 3; Time = 4: 3 -> 2 -> 5
两次Dijkstra ,使用数组分别记录每条路径中每个结点的前驱来记录路径,最后先判断路径是否相等再输出:
- #include
- #include
- #include
- #include
- #include
- #include
- using namespace std;
- int n, m, v1, v2, t, D, T, s, d;
- int dis[510], Time[510], cnt[510];
- int ansd[510], anst[510], cntd, cntt;
-
- int pred[510], pret[510]; //记录前驱
- struct edge {
- int next, dis, time;
- };
- vector
g[510]; - bool vis[510];
- priority_queue
int, int>, vectorint, int>>, greaterint, int>>>q; -
- void dfs_d(int x) {
- if (x == s) {
- ansd[cntd++] = x;
- return;
- }
- dfs_d(pred[x]);
- ansd[cntd++] = x;
- }
-
- void dfs_t(int x) {
- if (x == s) {
- anst[cntt++] = x;
- return;
- }
- dfs_t(pret[x]);
- anst[cntt++] = x;
- }
-
- int main() {
- cin >> n >> m;
- for (int i = 0; i < m; i++) {
- cin >> v1 >> v2 >> t >> D >> T;
- edge e;
- e.next = v2;
- e.dis = D;
- e.time = T;
- g[v1].push_back(e);
- if (t == 0) {
- e.next = v1;
- g[v2].push_back(e);
- }
- }
- cin >> s >> d;
- memset(dis, 127, sizeof(dis));
- memset(Time, 127, sizeof(Time));
- dis[s] = 0;
- Time[s] = 0;
- q.push(make_pair(0, s));
- while (!q.empty()) {
- int u = q.top().second;
- q.pop();
- if (vis[u]) {
- continue;
- }
- vis[u] = 1;
- for (int i = 0; i < g[u].size(); i++) {
- int v = g[u][i].next;
- if (dis[v] > dis[u] + g[u][i].dis) {
- dis[v] = dis[u] + g[u][i].dis;
- Time[v] = Time[u] + g[u][i].time;
- pred[v] = u;
- q.push(make_pair(dis[v], v));
- } else if (dis[v] == dis[u] + g[u][i].dis) {
- if (Time[v] > Time[u] + g[u][i].time) {
- Time[v] = Time[u] + g[u][i].time;
- pred[v] = u;
- q.push(make_pair(dis[v], v));
- }
- }
- }
- }
- memset(Time, 127, sizeof(Time));
- memset(cnt, 127, sizeof(cnt));
- memset(vis, 0, sizeof(vis));
- Time[s] = 0;
- cnt[s] = 0;
- q.push(make_pair(0, s));
- while (!q.empty()) {
- int u = q.top().second;
- q.pop();
- if (vis[u]) {
- continue;
- }
- vis[u] = 1;
- for (int i = 0; i < g[u].size(); i++) {
- int v = g[u][i].next;
- if (Time[v] > Time[u] + g[u][i].time) {
- Time[v] = Time[u] + g[u][i].time;
- cnt[v] = cnt[u] + 1;
- pret[v] = u;
- q.push(make_pair(Time[v], v));
- } else if (Time[v] == Time[u] + g[u][i].time) {
- if (cnt[v] > cnt[u] + 1) {
- cnt[v] = cnt[u] + 1;
- pret[v] = u;
- q.push(make_pair(Time[v], v));
- }
- }
- }
- }
- dfs_d(d);
- dfs_t(d);
- bool flag = 0;
- if (cntd == cntt) {
- for (int i = 0; i < cntd; i++) {
- if (ansd[i] != anst[i]) {
- flag = 1;
- break;
- }
- }
- } else {
- flag = 1;
- }
- if (flag) {
- cout << "Distance = " << dis[d] << ": ";
- for (int i = 0; i < cntd; i++) {
- cout << ansd[i];
- if (i != cntd - 1) {
- cout << " -> ";
- }
- }
- cout << endl;
- cout << "Time = " << Time[d] << ": ";
- for (int i = 0; i < cntt; i++) {
- cout << anst[i];
- if (i != cntt - 1) {
- cout << " -> ";
- }
- }
- } else {
- cout << "Distance = " << dis[d] << "; Time = " << Time[d] << ": ";
- for (int i = 0; i < cntd; i++) {
- cout << ansd[i];
- if (i != cntd - 1) {
- cout << " -> ";
- }
- }
- }
- return 0;
- }