找负环(图论基础)

文章目录

负环

环内路径上的权值和为负。

spfa找负环

两种基本的方法

  1. 统计每一个点的入队次数,如果一个点入队了n次,则说明存在负环
  2. 统计当前每个点中的最短路中所包含的边数,如果当前某个点的最短路所包含的边数大于等于n,也说明存在负环

实际上两种方法是等价的,都是判断是否路径包含n条边, n n n条边的话就有 n + 1 n+1 n+1个点

用的更多的还是第二种方法。

方法一

c n t [ x ] : 表示 x 的入队次数 cnt[x]:表示x的入队次数 cnt[x]:表示x的入队次数

cpp 复制代码
#include <bits/stdc++.h> 
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f*1ll

using namespace std;

void solve()
{
	int n,m1,m2;
	cin>>n>>m1>>m2;
	vector<vector<pii>>g(n+1);
	rep(i,1,m1){
		int u,v,w;
		cin>>u>>v>>w;
		g[u].pb({v,w});
		g[v].pb({u,w});
	}	
	rep(i,1,m2){
		int u,v,w;
		cin>>u>>v>>w;
		g[u].pb({v,-w});
	}
	vector<int>inq(n+1,0);
	vector<int>cnt(n+1,0);
	vector<int>d(n+1,0);
	queue<int>q;
	rep(i,1,n){
		q.push(i);
		inq[i]=1;
	}
	while(q.size()){
		auto t=q.front();
		q.pop();
		int u=t;
		inq[u]=0;
		for(auto it:g[u]){
			int v=it.x,w=it.y;
			if(d[v]>d[u]+w){
				d[v]=d[u]+w;
				if(!inq[v]){
					q.push(v);
					inq[v]=1;
					cnt[v]++;
					if(cnt[v]>=n){
					cout<<"YES"<<endl;
					return;
					}
				}
			}
		}
	}
	cout<<"NO"<<endl;
}

signed main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//   	freopen("1.in", "r", stdin);
	int _;
	cin>>_;
	while(_--)
	solve();
	return 0;
}

方法二

c n t [ x ] : 表示从起点到 x 所经过的最短路径的边数 cnt[x]:表示从起点到x所经过的最短路径的边数 cnt[x]:表示从起点到x所经过的最短路径的边数

cpp 复制代码
#include <bits/stdc++.h> 
#define int long long
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define fep(i,a,b) for(int i = (a); i >= (b); --i)
#define pii pair<int, int>
#define ll long long
#define db double
#define endl '\n'
#define x first
#define y second
#define pb push_back
#define inf 0x3f3f3f3f*1ll

using namespace std;

void solve()
{
	int n,m1,m2;
	cin>>n>>m1>>m2;
	vector<vector<pii>>g(n+1);
	rep(i,1,m1){
		int u,v,w;
		cin>>u>>v>>w;
		g[u].pb({v,w});
		g[v].pb({u,w});
	}	
	rep(i,1,m2){
		int u,v,w;
		cin>>u>>v>>w;
		g[u].pb({v,-w});
	}
	vector<int>inq(n+1,0);
	vector<int>cnt(n+1,0);
	vector<int>d(n+1,0);
	queue<int>q;
	rep(i,1,n){
		q.push(i);
		inq[i]=1;
	}
	while(q.size()){
		auto t=q.front();
		q.pop();
		int u=t;
		inq[u]=0;
		for(auto it:g[u]){
			int v=it.x,w=it.y;
			if(d[v]>d[u]+w){
				d[v]=d[u]+w;
				cnt[v]=cnt[u]+1;
				if(cnt[v]>=n){
					cout<<"YES"<<endl;
					return;
				}
				if(!inq[v]){
					q.push(v);
					inq[v]=1;
				}
			}
		}
	}
	cout<<"NO"<<endl;
}

signed main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//   	freopen("1.in", "r", stdin);
	int _;
	cin>>_;
	while(_--)
	solve();
	return 0;
}

实际效果


方法一跑出来的结果是 1024 m s 1024ms 1024ms

方法二跑出来的结果是 671 m s 671ms 671ms

相关推荐
╰つ゛木槿3 小时前
Spring Boot 调用DeepSeek API的详细教程
java·spring boot·后端·deepseek
Amd7943 小时前
FastAPI 自定义参数验证器完全指南:从基础到高级实战
fastapi·数据校验·安全性·api设计·参数验证·参数别名·field函数
程序视点4 小时前
SpringBoot配置入门
java·spring boot·spring
项目題供诗4 小时前
ES语法学习
学习·elasticsearch·django
岱宗夫up4 小时前
【Python】Django 中的算法应用与实现
数据库·python·opencv·django·sqlite
天道有情战天下5 小时前
python flask
开发语言·python·flask
XORE956 小时前
IDEA Generate POJOs.groovy 踩坑小计 | 生成实体 |groovy报错
java·spring·intellij-idea
heart000_16 小时前
基于SpringBoot的智能问诊系统设计与隐私保护策略
java·spring boot·后端
小杨4047 小时前
springboot框架项目实践应用三(监控运维组件admin)
spring boot·后端·监控
Y雨何时停T7 小时前
Spring IoC 详解
java·spring·rpc