spfa求负环问题

文章介绍了如何运用SPFA算法解决洛谷平台上的三个编程题,涉及负环检测、带虫洞的网络连通性和两个角色间的最短距离计算,强调了处理特值和算法应用的重要性。
摘要由CSDN通过智能技术生成

下面分享3道简单题主要是熟悉spfa,和在求负环上的应用。

题一:洛谷P3385 【模板】负环

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 2010, M = 2*3010;
int h[M], ne[M], e[M], w[M];
int cnt[N];
int d[N];
bool st[N];
int n, m;
int t;
int idx;
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa()
{
	memset(d, 0x3f, sizeof(d));
	memset(st, false, sizeof(st));
	memset(cnt, 0, sizeof(cnt));
	queue<int >q;
	/*for (int i = 1;i <= n;i++) {
		q.push(i);
		st[i] = true;
	}*/
	q.push(1);
	st[1]=true;
	d[1]=0;
	while (q.size())
	{
		int o = q.front();
		q.pop();
		st[o] = false;
		for (int i = h[o];i != -1;i = ne[i]) {
			int j = e[i];
			if (d[j] > d[o] + w[i]) {
				d[j] = d[o] + w[i];
				cnt[j] = cnt[o] + 1;
				if (cnt[j] == n) {
					return true;
				}
				if (st[j]) {
					continue;
				}
				q.push(j);
				st[j] = true;
			}
		}
	}
	return false;
}
int main()
{
	scanf("%d", &t);
	while (t--) {
		memset(h, -1, sizeof(h));
		idx = 0;
		scanf("%d%d", &n, &m);
		for (int i = 0;i < m;i++) {
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			if (c >= 0) {
				add(a, b, c);
				add(b, a, c);
			}
			else {
				add(a, b, c);
			}
		}
		if (spfa()) {
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
}

这题没有特值,不需要考虑能否从1到n。

题二:洛谷P2850 [USACO06DEC] Wormholes G(虫洞)

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 510, M = 5500;
int h[M], ne[M], e[M], w[M];
int d[N];
int cnt[N];
bool st[N];
int idx, n, m, t;
int w1;
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa()
{
	memset(st, false, sizeof(st));
	memset(d, 0, sizeof(d));
	memset(cnt, 0, sizeof(cnt));
	queue <int>q;
	for (int i = 1;i <= n;i++) {
		q.push(i);
		st[i] = true;
	}
	while (q.size()) {
		int o = q.front();
		q.pop();
		st[o] = false;
		for (int i = h[o];i != -1;i = ne[i]) {
			int j = e[i];
			if (d[j] > d[o] + w[i]) {
				d[j] = d[o] + w[i];
				cnt[j] = cnt[o] + 1;
				if(cnt[j] == n) {
					return true;
				}
				if (st[j]) {
					continue;
				}
				q.push(j);
				st[j] = true;
			}
		}
	}
	return false;
}
int main()
{
	scanf("%d", &t);
	while (t--)
	{
		memset(h, -1, sizeof(h));
		idx = 0;
		scanf("%d%d%d", &n, &m,&w1);
		for (int i = 0;i < m;i++) {
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			add(a, b, c);
			add(b, a, c);
		}
		for (int i = 0;i < w1;i++)
		{
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			add(a, b, -c);

		}
		if (spfa())
		{
			printf("YES\n");
		}
		else {
			printf("NO\n");
		}
	}
}

这题存在特值,即前面的点可能不会走向虫洞,需要引入一个源点,将d初始化为0,即所有点都将以0为权联通,这样就一定会走向虫洞。

题三:洛谷P2136 拉近距离

这题同样特值,需要考虑小明和小红两个人分别用spfa,(爱情是两个人的事),没有考虑到这一点,就只有90分,(正所谓只有一个人付出的爱情不会圆满)。

感觉这题还是相当有意思,充满哲理。

好了为了小明和小红的forever love,开始练习吧。

代码:

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 10010;
int h[M], e[M], ne[M], w[M];
int d[N];
int cnt[N];
bool st[N];
int n, m, idx;
void add(int a, int b, int c) {
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
bool spfa()
{
	memset(d, 0x3f, sizeof(d));
	memset(st, false, sizeof(st));
	memset(cnt, 0, sizeof(cnt));
	queue<int> q;
	q.push(n);
	st[n] = true;
	d[n] = 0;
	while (q.size())
	{
		int o = q.front();
		q.pop();
		st[o] = false;
		for (int i = h[o];i != -1;i = ne[i]) {
			int j = e[i];
			if (d[j] > d[o] + w[i])
			{
				d[j] = d[o] + w[i];
				cnt[j] = cnt[o] + 1;
				if (cnt[j] == n) {
					return true;
				}
				if (st[j]) {
					continue;
				}
				q.push(j);
				st[j] = true;
			}
		}
		
	}
	return false;
}
bool spfa2()
{
	memset(d, 0x3f, sizeof(d));
	queue<int> q;
	q.push(1);
	st[1] = true;
	d[1] = 0;
	while (q.size())
	{
		int o = q.front();
		q.pop();
		st[o] = false;
		for (int i = h[o];i != -1;i = ne[i]) {
			int j = e[i];
			if (d[j] > d[o] + w[i])
			{
				d[j] = d[o] + w[i];
				cnt[j] = cnt[o] + 1;
				if (cnt[j] == n) {
					return true;
				}
				if (st[j]) {
					continue;
				}
				q.push(j);
				st[j] = true;
			}
		}
	}
	return false;
}
int main()
{
	scanf("%d%d", &n, &m);
	memset(h, -1, sizeof(h));
	for (int i = 0;i < m;i++) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		add(a, b, -c);
	}
	if (spfa2()||spfa()) {
		printf("Forever love");
	}
	else {
		spfa();
		int s = d[1];
		spfa2();
		int s1 = d[n];
		printf("%d", min(s,s1));
	}
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值