SPFA+队列优化与迪杰斯特拉+优先队列(最小堆优化)解一道水题

49 篇文章 0 订阅

其实算法还是挺有意思的,可惜我年纪大了,搞不动了,这种东西应该适合在中学时候搞。。

题目:

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤500) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

Sample Output:

0 2 3 3 40

题意:英文比较简单,不翻译了,就是给一个图,有距离有边权,求最短路径而且不唯一时输出边权唯一的路径。

解答:方法一:SPFA+队列优化,具体百度。只讲边权如何处理,在最短距离相同时候去看权值大小比较得出结果,如果更优,则更新,同时用一个数组存下每个节点的路径前驱,每次更新时候不要忘记更新。

​#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string.h>
using namespace std;
const int MAXN = 505;
vector<pair<int, int>> E[MAXN];
int n, m;
int d[MAXN];
int inq[MAXN] = {0};
int pre[MAXN];
int costMin[MAXN];
vector<pair<int, int> > cost[MAXN];
int main() {
	int s, t;
	scanf("%d%d%d%d", &n, &m, &s, &t);
	fill(d, d + n, 1e9);
	fill(costMin, costMin + n, 1e9);
	for (int i = 0; i < m; i++) {
		int a, b, c, d;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		E[a].push_back( make_pair(b,c));
		E[b].push_back(make_pair(a, c));
		cost[a].push_back(make_pair(b, d));
		cost[b].push_back(make_pair(a, d));
	}
	queue<int> Q;
	Q.push(s);
	d[s] = 0;
	costMin[s] = 0;
	inq[s] = 1;
	while (!Q.empty()) {
		int now = Q.front();
		Q.pop();
		inq[now] = 0;
		for (int i = 0; i < E[now].size(); i++) {
			int v = E[now][i].first;
			if (d[v] > d[now] + E[now][i].second) {
				if (inq[v] == 1)continue;
				d[v] = d[now] + E[now][i].second;
				costMin[v] = costMin[now] + cost[now][i].second;
				pre[v] = now;
				inq[v] = 0;
				Q.push(v);
			}
			else if (d[v] == d[now] + E[now][i].second) {
				if (costMin[now] + cost[now][i].second < costMin[v]) {
					costMin[v] = costMin[now] + cost[now][i].second;
					pre[v] = now;
				}
			}
		}
	}
	vector<int> tempPath;
	int ttt = t;
	tempPath.push_back(t);
	while (ttt != s) {
		ttt = pre[ttt];
		tempPath.push_back(ttt);
	}
	for (int i = tempPath.size() - 1; i >= 0; i--) {
		printf("%d ", tempPath[i]);
	}
	printf("%d %d\n", d[t], costMin[t]);
	return 0;
}

方法二:迪杰斯特拉+优先队列

和上面基本一样,就是用的方法不一样,基本上就改改就好了

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<string.h>
using namespace std;
const int MAXN = 505;
vector<pair<int, int>> E[MAXN];
int n, m;
int d[MAXN];
int pre[MAXN];
int costMin[MAXN];
vector<pair<int, int> > cost[MAXN];
int main() {
	int s, t;
	scanf("%d%d%d%d", &n, &m, &s, &t);
	fill(d, d + n, 1e9);
	fill(costMin, costMin + n, 1e9);
	for (int i = 0; i < m; i++) {
		int a, b, c, d;
		scanf("%d%d%d%d", &a, &b, &c, &d);
		E[a].push_back( make_pair(b,c));
		E[b].push_back(make_pair(a, c));
		cost[a].push_back(make_pair(b, d));
		cost[b].push_back(make_pair(a, d));
	}
	priority_queue<pair<int, int>> Q; d[s] = 0;
	Q.push(make_pair(-d[s], s));// 小顶堆返回最小
	costMin[s] = 0;
	while (!Q.empty()) {
		int now = Q.top().second;
		Q.pop();
		for (int i = 0; i < E[now].size(); i++) {
			int v = E[now][i].first;
			if (d[v] > d[now] + E[now][i].second) {
				d[v] = d[now] + E[now][i].second;
				costMin[v] = costMin[now] + cost[now][i].second;
				pre[v] = now;
				Q.push(make_pair(-d[v], v));
			}
			else if (d[v] == d[now] + E[now][i].second) {
				if (costMin[now] + cost[now][i].second < costMin[v]) {
					costMin[v] = costMin[now] + cost[now][i].second;
					pre[v] = now;
				}
			}
		}
	}
	vector<int> tempPath;
	int ttt = t;
	tempPath.push_back(t);
	while (ttt != s) {
		ttt = pre[ttt];
		tempPath.push_back(ttt);
	}
	for (int i = tempPath.size() - 1; i >= 0; i--) {
		printf("%d ", tempPath[i]);
	}
	printf("%d %d\n", d[t], costMin[t]);
	return 0;
}

总结:当然了,最后找路径用DFS也是可以的,看个人喜好,反正吧,最短路如果这两种还不行。。别的方法应该是没什么希望了。反正就这样了,如果要负环的话,那就SPFA,没有就无脑迪杰斯特拉。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值