其他分享
首页 > 其他分享> > Bellman-Ford(贝尔曼—福特)

Bellman-Ford(贝尔曼—福特)

作者:互联网

Bellman-Ford(贝尔曼—福特)

时间复杂度O(nm)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define pll pair<ll,ll>
const int mod=1e9+7;
const int N=1e3+9,M=1e4+9;
int a[N],x[M],y[M],z[M];
int n,m;
void f0(int T)
{
	sf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
		sf("%d%d%d",&x[i],&y[i],&z[i]);
	for(int i=1;i<=n;i++)
		a[i]=1e9;
	a[1]=0;
	for(int j=1;j<n;j++)
		for(int i=1;i<=m;i++)
		{
			a[y[i]]=min(a[y[i]],a[x[i]]+z[i]);//从x到y
			a[x[i]]=min(a[x[i]],a[y[i]]+z[i]);//从y到x
		}
	pf("%d\n",a[n]);
}
int main()
{
	int T=1;
//	sf("%d",&T);
	for(int i=1;i<=T;i++)
        f0(i);
    return 0;
}

SPFA(Bellman-Ford队列优化)

时间复杂度O(km),最大O(nm)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define pll pair<ll,ll>
const int mod=1e9+7;
const int N=1e3+9,M=2e4+9;
struct pp
{
	int x,y,z;
}p[M];
bool book[N];
int h[M],b[N];
int n,m,k;
void SPFA()
{
    queue<int>q;
    for(int i=1;i<=n;i++)
    {
		b[i]=1e9;
		book[i]=false;
	}	
	b[1]=0;
	book[1]=true;
	q.push(1);
    while(q.size())
	{
        int x=q.front();
        q.pop();
        book[x]=false;
        for(int i=h[x];i!=-1;i=p[i].x)
		{
            int y=p[i].y;
            if(b[y]>b[x]+p[i].z)
			{
                b[y]=b[x]+p[i].z;
                if(!book[y])
                {
					q.push(y);
					book[y]=true;
				}
            }
        }
    }
}
void add(int x,int y,int z)
{
	p[k].x=h[x];
	p[k].y=y;
	p[k].z=z;
	h[x]=k++;
}
void f0(int T)
{
	k=0;
	memset(h,-1,sizeof h);
	sf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		sf("%d%d%d",&x,&y,&z);
		add(x,y,z);
		add(y,x,z);
	}
	SPFA();
	pf("%d\n",b[n]);
}
int main()
{
	int T=1;
//	sf("%d",&T);
	for(int i=1;i<=T;i++)
        f0(i);
    return 0;
}

SPFA(Bellman-Ford队列优化)判断负环

时间复杂度O(nm)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define pll pair<ll,ll>
const int mod=1e9+7;
const int N=1e4+9,M=1e5+9;
struct pp
{
	int x,y,z;
}p[M];
bool book[N];
int h[M],b[N],c[N];
int n,m,k;
bool SPFA()
{
    queue<int>q;
    for(int i=1;i<=n;i++)
    {
		b[i]=1e9;
		c[i]=0;  	
		book[i]=false;
	}
	b[1]=0;
	book[1]=true;
	q.push(1);
    while(q.size())
	{
        int x=q.front();
        q.pop();
        book[x]=false;
        for(int i=h[x];i!=-1;i=p[i].x)
		{
            int y=p[i].y;
            if(b[y]>b[x]+p[i].z)
			{
                b[y]=b[x]+p[i].z;
                c[y]=c[x]+1;
                if(c[y]>=n)
                	return true;
                if(!book[y])
                {
					q.push(y);
					book[y]=true;
				}
            }
        }
    }
    return false;
}
void add(int x,int y,int z)
{
	p[k].x=h[x];
	p[k].y=y;
	p[k].z=z;
	h[x]=k++;
}
void f0(int T)
{
	k=0;
	memset(h,-1,sizeof h);
	sf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		int x,y,z;
		sf("%d%d%d",&x,&y,&z);
		add(x,y,z);
		if(z>=0)
			add(y,x,z);
	}
	if(SPFA())
		pf("YES\n");
	else
		pf("NO\n");
}
int main()
{
	int T=1;
//	sf("%d",&T);
	for(int i=1;i<=T;i++)
        f0(i);
    return 0;
}

 

标签:const,int,Bellman,Ford,book,long,贝尔曼,sf,define
来源: https://www.cnblogs.com/2021sgy/p/16601659.html