1111 Online Map (30分) [Dijkstra]

121 篇文章 0 订阅
115 篇文章 0 订阅

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

题解:

直接两遍迪杰斯特拉 over

#include <iostream>
#include <cstring>
#include <vector>

using namespace std;

const int N = 510;

int n, m, start, target;
int d[N][N], d2[N][N], dist[N], dist2[N], cost[N], pred[N], pret[N], cnt[N];
bool st[N];

void Dijkstra1() {
	memset(dist, 0x3f, sizeof(dist));
	dist[start] = 0;

	for (int i = 0; i < n; i++)
	{
		int t = -1;
		for (int j = 0; j < n; j++)
			if (!st[j] && (t == -1 || dist[j] < dist[t]))
				t = j;
		st[t] = true;

		for (int j = 0; j < n; j++)
		{
			if (dist[j] > dist[t] + d[t][j])
			{
				dist[j] = dist[t] + d[t][j];
				cost[j] = cost[t] + d2[t][j];
				pred[j] = t;
			}
			else if (dist[j] == dist[t] + d[t][j])
			{
				if (cost[j] > cost[t] + d2[t][j])
				{
					cost[j] = cost[t] + d2[t][j];
					pred[j] = t;
				}
			}
		}
	}
}
void Dijkstra2() {
	memset(dist2, 0x3f, sizeof(dist2));
	memset(st, false, sizeof(st));
	dist2[start] = 0;
	cnt[start] = 1;
	for (int i = 0; i < n; i++)
	{
		int t = -1;
		for (int j = 0; j < n; j++)
			if (!st[j] && (t == -1 || dist2[j] < dist2[t]))
				t = j;
		st[t] = true;

		for (int j = 0; j < n; j++)
		{
			if (dist2[j] > dist2[t] + d2[t][j])
			{
				dist2[j] = dist2[t] + d2[t][j];
				cnt[j] = cnt[t] + 1;
				pret[j] = t;
			}
			else if (dist2[j] == dist2[t] + d2[t][j])
			{
				if (cnt[j] > cnt[t] + 1)
				{
					cnt[j] = cnt[t] + 1;
					pret[j] = t;
				}
			}
		}
	}
}

int main() {

	cin >> n >> m;

	memset(d, 0x3f, sizeof(d));
	memset(d2, 0x3f, sizeof(d2));

	for (int i = 0; i < m; i++)
	{
		int v1, v2, oneway, l, t;
		cin >> v1 >> v2 >> oneway >> l >> t;
		if (oneway == 1) {
			d[v1][v2] = l;
			d2[v1][v2] = t;
		}
		else
		{
			d[v1][v2] = d[v2][v1] = l;
			d2[v1][v2] = d2[v2][v1] = t;
		}
	}
	cin >> start >> target;

	Dijkstra1();
	Dijkstra2();
	vector<int> pd, pt;
	for (int i = target; i != start; i = pred[i]) pd.push_back(i);
	for (int i = target; i != start; i = pret[i]) pt.push_back(i);

	if (pd == pt)
	{
		printf("Distance = %d; Time = %d: %d", dist[target], dist2[target], start);
		for (int i = pd.size() - 1; i >= 0; i--)
			printf(" -> %d", pd[i]);
	}
	else
	{
		printf("Distance = %d: %d", dist[target], start);
		for (int i = pd.size() - 1; i >= 0; i--)
			printf(" -> %d", pd[i]);
		cout << endl;
		printf("Time = %d: %d", dist2[target], start);
		for (int i = pt.size() - 1; i >= 0; i--)
			printf(" -> %d", pt[i]);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Dijkstra算法是一种用于解决最短路径问题的贪心算法。该算法的基本思想是从起始节点开始,通过不断扩展当前已找到的最短路径来逐步确定最短路径的结果。 具体的实现步骤如下: 1. 初始化:设置起始节点为当前节点,将起始节点到自身的距离为0,将起始节点到其他节点的距离设为无穷大。 2. 判断是否遍历了所有节点:如果还有未处理的节点,则继续执行下述步骤。否则,算法结束。 3. 遍历邻接节点:对于当前节点的所有邻接节点,计算经过当前节点到达该邻接节点的距离。如果该距离小于已确定的最短距离,则更新最短距离。 4. 选择下一个节点:从未处理的节点中选取距离起始节点最近的节点作为下一个节点。 5. 将选择的节点标记为处理完成。 6. 跳转至步骤2。 通过以上步骤,Dijkstra算法可以得到从起始节点到图中所有其他节点的最短路径。在实际应用中,可以使用优先队列来高效地实现步骤4的节点选择操作。 然而,需要注意的是,Dijkstra算法对于存在负权边的图无法正确处理,因为它会假设经过已处理的节点的路径是最短路径。如果图中存在负权边,可以使用Bellman-Ford算法来解决。此外,Dijkstra算法的时间复杂度为O(V^2),其中V表示节点的个数。若要减少时间复杂度,可以使用堆优化的Dijkstra算法,其时间复杂度为O((V+E)logV),其中E表示边的个数。 总之,Dijkstra算法是一种解决最短路径问题的有效算法,通过不断扩展已找到的最短路径来逐步确定最短路径的结果。在实际应用中,可以根据具体情况选择不同的优化策略。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值