跳到正文

个人记录

负环

liuyilun123约 8 分钟
原文

这是 2026年5月4日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年4月27日
收录
2026年5月4日
更新
2026年11月30日
篇幅
约 8 分钟
获赞
1

版本

  1. v3当前
  2. v2
  3. v1

负环的定义:

就是在图中存在一个环 ,且环的边权和为负数 ,那么我们就称他为负环 。负环中没有最短路 。

整么判断负环 :

定义一个数组 cnt[] 表示这条边经过的次数 , 用 spfaspfa 在松弛的时候更新一下 cnt ,如果当 cnt 大于 n−1n - 1 时就一定有负环(因为只多有这么多条边)。

P2036 [COCI 2008/2009 #2] PERKET

思路:

思路跟判断负环一样我就不说了 。

代码:

C++
#include<bits/stdc++.h>

using namespace std;

int n, s[15], b[15], mini = 1e9, suan = 1, ku;

void dfs(int x)
{
	if(x > n)
	{
		if(!(suan == 1 && ku == 0))
		{
			mini = min(mini, abs(suan - ku));
		}
		return ;
	}

	suan *= s[x];
	ku += b[x];
	dfs(x + 1);
	suan /= s[x];
	ku -= b[x];

	dfs(x + 1);
}

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)
	{
		cin >> s[i] >> b[i];
	}

	dfs(1);

	cout << mini;
	return 0;
}

P1931 套利

思路:

就是把求负环变成了求正环 ,注意这次边权都是乘 ,且要枚举每个起点 。

代码:

C++
#include<bits/stdc++.h>

using namespace std;

int n, m;
double dis[35];
int cnt[35];
bool vis[35];
map<string, int> a;

struct Node
{
	int x;
	double w;

	bool operator < (const Node & tmp) const
	{
		return w > tmp.w;
	}
};
vector<Node> g[35];

bool spfa(int s)
{
	memset(dis, 0, sizeof dis);
	memset(cnt, 0, sizeof cnt);
	memset(vis, 0, sizeof vis);
	queue<int> q;

	q.push(s);
	dis[s] = 1;
	cnt[s] = 0;
	vis[s] = 1;

	while(q.empty() == 0)
	{
		int c = q.front();
		q.pop();
		vis[c] = 0;

		for(int i = 0; i < g[c].size(); i++)
		{
			if(dis[g[c][i].x] < dis[c] * g[c][i].w)
			{
				dis[g[c][i].x] = dis[c] * g[c][i].w;
				cnt[g[c][i].x] = cnt[c] + 1;

				if(cnt[g[c][i].x] == n)
				{
					return 1;
				}

				if(vis[g[c][i].x] == 0)
				{
					vis[g[c][i].x] = 1;
					q.push(g[c][i].x);
				}
			}
		}
	}

	return 0;
}

int main()
{
	int cnt = 0;

	while(cin >> n)
	{
		cnt++;

		if(n == 0)
		{
			return 0;
		}

		for(int i = 1; i <= n; i++)
		{
			string s;
			cin >> s;
			a[s] = i;
			g[a[s]].clear();
		}

		cin >> m;

		for(int i = 1; i <= m; i++)
		{
			string s1, s2;
			double x;
			cin >> s1 >> x >> s2;
			g[a[s1]].push_back({a[s2], x});
		}

		string o = "No";

		cout << "Case " << cnt << ": ";

		for(int i = 1; i <= n; i++)
		{
			bool v = spfa(i);
			if(v == 1)
			{
				o = "Yes";
				break;
			}
		}

		cout << o << "\n";
	}
	return 0;
}

P2850 [USACO06DEC] Wormholes G

思路:

虫洞的边权就是负边值 ,我们只需要在整张图中找到一个负环即可 。注意路径的耗时要取最小值 。

代码:

C++
#include<bits/stdc++.h>

#define int long long

using namespace std;

int f, n, m, w, dis[2505], cnt[2505], a[505][505];
bool vis[2505];

struct Node
{
	int x, w;

	bool operator < (const Node & tmp) const
	{
		return w > tmp.w;
	}
};
vector<Node> g[2505];

bool spfa(int s)
{
	memset(dis, 0x3f, sizeof dis);
	memset(vis, 0, sizeof vis);
	memset(cnt, 0, sizeof cnt);
	queue<int> q;

	dis[s] = 0;
	vis[s] = 1;
	cnt[s] = 0;
	q.push(s);

	while(q.empty() == 0)
	{
		int c = q.front();
		q.pop();
		vis[c] = 0;

		for(int i = 0; i < g[c].size(); i++)
		{
			if(dis[g[c][i].x] > dis[c] + g[c][i].w)
			{
				dis[g[c][i].x] = dis[c] + g[c][i].w;
				cnt[g[c][i].x] = cnt[c] + 1;

				if(cnt[g[c][i].x] == n)
				{
					return 1;
				}

				if(vis[g[c][i].x] == 0)
				{
					vis[g[c][i].x] = 1;
					q.push(g[c][i].x);
				}
			}
		}
	}

	return 0;
}

signed main()
{
	cin >> f;

	while(f--)
	{
		cin >> n >> m >> w;

		for(int i = 1; i <= n; i++)
		{
			g[i].clear();
		}

		memset(a, 0x3f, sizeof a);
		for(int i = 1; i <= m; i++)
		{
			int x, y, t;
			cin >> x >> y >> t;
			a[x][y] = a[y][x] = min(a[x][y], t);
			g[x].push_back({y, a[x][y]});
			g[y].push_back({x, a[x][y]});
		}

		for(int i = 1; i <= w; i++)
		{
			int x, y, t;
			cin >> x >> y >> t;
			g[x].push_back({y, -t});
		}

		string s = "NO";

		for(int i = 1; i <= n; i++)
		{
			bool v = spfa(i);
			if(v == 1)
			{
				s = "YES";
				break;
			}
		}

		cout << s << "\n";
	}
	return 0;
}