1111 Online Map (30 分)(纯粹的迪杰斯特拉,详解)

360 篇文章 3 订阅
91 篇文章 1 订阅

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

结尾无空行

如果本题能够独立写完,并且能做对,说明几乎任何 迪杰斯特拉求最短路径的问题就都是小菜了,以下是详解:

#include<iostream>
#include<bits/stdc++.h>
#include<string>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<deque>
#include<unordered_set>
#include<unordered_map>
#include<cctype>
#include<algorithm>
#include<stack>
using namespace std;
const int N = 501;
int T[N][N];// 存储时间
int D[N][N];// 存储举例
int vis[N];// 判断是否访问过
int counts[N];// 当时间相等,求经过的最短街道
int distence[N];// 存放起点到各点的最短路径
int times[N];//起点到各点的最短时间
int n, m;// n 和 m
int pre1[N];// 存放前缀路径
int pre2[N];
vector<int> path1;// 存放路径
vector<int> path2;
const int inf = 1e9;
void init() {
	fill(T[0], T[0] + N * N, inf);
	fill(D[0], D[0] + N * N, inf);
	memset(vis, 0, sizeof(vis));
	fill(counts, counts + N, 1);
}
void Dijkstra1(int start) {// 求最短路径
	fill(distence, distence + N, inf);
	for (int i = 0; i < n; i++) {
		pre1[i] = i;
	}
	distence[start] = 0;
	for (int i = 0; i < n; i++) {
		int u = -1, minn = inf;
		for (int j = 0; j < n; j++) {
			if (!vis[j] && distence[j] < minn) {
				u = j;
				minn = distence[j];
			}
		}
		if (u == -1) {
			break;
		}
		vis[u] = true;
		for (int v = 0; v < n; v++) {
			if (!vis[v] && D[u][v] != inf) {
				if (D[u][v] + distence[u] < distence[v]) {// 更新路径
					distence[v] = D[u][v] + distence[u];
					times[v] = times[u] + T[u][v];
					pre1[v] = u;
				} else if (D[u][v] + distence[u] == distence[v]){// 路径相同
					if (times[v] > times[u] + T[u][v]) {// 更新最短时间
						times[v] = times[u] + T[u][v];
						pre1[v] = u;
					}
				}
				
			}
		}
	}
}
void Dijkstra2(int start) {// 求最短时间
	fill(times, times + N, inf);
	memset(vis, 0, sizeof(vis));
	for (int i = 0; i < n; i++) {
		pre2[i] = i;
	}
	times[start] = 0;
	for (int i = 0; i < n; i++) {
		int u = -1, minn = inf;
		for (int j = 0; j < n; j++) {
			if (!vis[j] && times[j] < minn) {
				minn = times[j];
				u = j;
			}
		}
		if (u == -1) {
			break;
		}
		vis[u] = true;
		for (int v = 0; v < n; v++) {
			if (!vis[v] && T[u][v] != inf) {
				if (times[v] > times[u] + T[u][v]) {// 更新最短时间
					times[v] = times[u] + T[u][v];
					counts[v] += counts[u];
					pre2[v] = u;
				} else if (times[v] == times[u] + T[u][v]) {// 时间相同
					if (counts[v] > counts[u]) {
						counts[v] = counts[u];// 求经过最短街道
						pre2[v] = u;
					}
				}
				
			}
		}
	}
}
void findpath1(int start, int x) {
	if (x == start) {// start 是起点, x 是当前节点,如果相等,输出
		path1.push_back(x);
		return;
	}
	findpath1(start, pre1[x]);// 不相等,继续深搜前驱结点
	path1.push_back(x);
}
void findpath2(int start, int x) {
	if (x == start) {
		path2.push_back(x);
		return;
	}
	findpath2(start, pre2[x]);
	path2.push_back(x);
}
int main() {
	cin >> n >> m;
	init();
	for (int i = 0; i < m; i++) {
		int v1, v2, one_way, length, time;
		cin >> v1 >> v2 >> one_way >> length >> time;
		if (one_way == 0) {
			D[v1][v2] = length;
		    D[v2][v1] = length;
		    T[v1][v2] = time;
		    T[v2][v1] = time;
		} else {
			D[v1][v2] = length;
			T[v1][v2] = time;
		}
	}
	int start, end;
	cin >> start >> end;
	Dijkstra1(start);
	Dijkstra2(start);
	findpath1(start, end);
	findpath2(start, end);
	bool flag = true;
	for (int i = 0; i < path1.size() && i < path2.size(); i++) {
		if (path1[i] != path2[i]) {
			flag = false;
			break;
		}
	}
	if (path1.size() != path2.size()) {
		flag = false;
	}
	if (flag) {
		printf("Distance = %d; Time = %d: ", distence[end], times[end]);
		for (int i = 0; i < path2.size(); i++) {
			if (!i) {
				cout << path2[i];
			} else {
				cout << " -> " << path2[i];
			}
		}
	} else {
		printf("Distance = %d: ", distence[end]);
		for (int i = 0; i < path1.size(); i++) {
			if (!i) {
				cout << path1[i];
			} else {
				cout << " -> " << path1[i];
			}
		}
		cout << endl;
		printf("Time = %d: ", times[end]);
		for (int i = 0; i < path2.size(); i++) {
			if (!i) {
				cout << path2[i];
			} else {
				cout << " -> " << path2[i];
			}
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_努力努力再努力_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值