C++ 最短路( spfa 判断负环)

spfa 判断负环

题目描述

给定一个 n 个点 m 条边的有向图,图中可能存在重边和自环, 边权可能为负数。

请你判断图中是否存在负权回路。

输入

第一行包含整数 n 和 m。

接下来 m 行每行包含三个整数 x,y,z。表示存在一条从点 x 到点 y 的有向边,边长为 z。

输出

如果图中存在负权回路,则输出 Yes,否则输出 No。

方法一:

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define int long long 
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
  
using namespace std;
 
const int N = 1e6+10;
 
int n,m,A;
int dis[N];
int vis[N]; 
int cnt[N];
vector<pair<int,int >> e[N];
 
int spfa()
{
    for(int i = 1;i<=n;i++) dis[i] = 1e18;
    queue<int > q;
    q.push(1);
    dis[1] = 0;
    vis[1] = 1;
    while(q.size())
    {
        int now = q.front();
        q.pop();
        vis[now] = 0;
        for(auto t:e[now])
        {
            int spot = t.first,w = t.second;
            if(dis[spot]>dis[now]+w)
            {
                cnt[spot] = cnt[now]+1;
                if(cnt[spot]>=n) return false;
                dis[spot] = dis[now]+w;
                if(vis[spot] == 0)
                {
                    vis[spot] = 1;
                    q.push(spot);
                } 
            }
        }
         
    }
    return true;
}
 
signed main()
{
    IOS;
    cin>>n>>m;
    while(m--)
    {
        int a,b,c;
        cin>>a>>b>>c;
        e[a].push_back({b,c});
    }
    spfa()?cout<<"No":cout<<"Yes";
     
    return 0;
}

方法二:

#include<bits/stdc++.h>
#define int long long
 
using namespace std;
 
const int N = 1e5+10;
 
int n,m,A;
int dis[N];
int vis[N];
int cnt[N];
 
vector<pair<int,int> > e[N];
 
int spfa()
{
    for(int i=1;i<=n;i++) dis[i] = 1e18;
    queue<int > q;
    q.push(n+1);
    dis[n+1] = 0;
    vis[n+1] = 1;
    while(q.size())
    {
        int now = q.front();
        q.pop();vis[now] = 0;
        for(auto t:e[now])
        {
            int spot = t.first,w = t.second;
            if(dis[spot]>dis[now]+w)
            {
                cnt[spot] = cnt[now]+1;
                if(cnt[spot]>=n+1) return false;
                dis[spot] = dis[now]+w;
                if(vis[spot]==0)
                {
                    vis[spot] = 1;
                    q.push(spot);
                }
            }
        }
    }
    return true;
}
 
signed main()
{
    cin>>n>>m;
    while(m--)
    {
        int a,b,c;
        cin>>a>>b>>c;
        e[a].push_back({b,c});
    }
    for(int i=1;i<=n;i++)
    {
        e[n+1].push_back({i,0});
    }
    spfa()?cout<<"No":cout<<"Yes";
}

超级原点(有不连通情况时):

双向边:

for(int i = 1;i<=n;i++)
	{
		e[n+1].push_back({i,0});
		e[i].push_back({n+1,0});
	} 

单向边:

 for(int i=1;i<=n;i++)
    {
        e[n+1].push_back({i,0});
    }

【模板】负环

题目描述

给定一个 n 个点的有向图,请求出图中是否存在从顶点 1 出发能到达的负环。

负环的定义是:一条边权之和为负数的回路。

输入格式

本题单测试点有多组测试数据

输入的第一行是一个整数 T,表示测试数据的组数。对于每组数据的格式如下:

第一行有两个整数,分别表示图的点数 nn 和接下来给出边信息的条数 mm。

接下来 m 行,每行三个整数 u,v,w。

  • 若 w≥0,则表示存在一条从 u 至 v 边权为 w 的边,还存在一条从 v 至 u 边权为 w 的边。
  • 若 w<0,则只表示存在一条从 u 至 v 边权为 w 的边。

输出格式

对于每组数据,输出一行一个字符串,若所求负环存在,则输出 YES,否则输出 NO

#include<bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define int long long 
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
 
using namespace std;

const int N = 1e6+10;

int n,m,A;
int dis[N];
int vis[N]; 
int cnt[N];
vector<pair<int,int >> e[N];

int spfa()
{
	queue<int > q;
	q.push(1);
	dis[1] = 0;
	vis[1] = 1;
	while(q.size())
	{
		int now = q.front();
		q.pop();
		vis[now] = 0;
		for(auto t:e[now])
		{
			int spot = t.first,w = t.second;
			if(dis[spot]>dis[now]+w)
			{
				cnt[spot] = cnt[now]+1;
				if(cnt[spot]>=n) return false;
				dis[spot] = dis[now]+w;
				if(vis[spot] == 0)
				{
					vis[spot] = 1;
					q.push(spot);
				} 
			}
		}
		
	}
	return true;
}

signed main()
{
	IOS;
	int t;
	int a,b,c;
	cin>>t;
	while(t--)
	{
		cin>>n>>m;
		for(int i = 1;i<=n;i++)
		{
			e[i].clear();
			dis[i] = 1e18;
			vis[i] = 0;
            cnt[i] = 0;
		}
		while(m--)
		{
			
			cin>>a>>b>>c;
			if(c>=0) 
			{
				e[a].push_back({b,c});
				e[b].push_back({a,c});
			}
			else e[a].push_back({b,c});
		}
		spfa()?cout<<"NO"<<"\n":cout<<"YES"<<"\n";
	}
	
	
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值