Bellman-For

推荐看这篇文章
Bellman-Ford算法可以判断负环,计算权值有负数的情况,
SPFA算法只能判断负环,不能输出负圈。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<sstream>
using namespace std;
typedef long long ll;
const int maxn = 1000 + 10;
const int INF = 1 << 25;
int T, n, m, cases;
struct edge
{
    int u, v, w;
};
edge a[maxn];
int path[maxn], d[maxn];
bool Bellman(int v0)
{
    for(int i = 0; i < n; i++)
        d[i] = INF, path[i] = -1;
    d[v0] = 0;
    for(int i = 0; i < n; i++)//迭代n次,如果第n次还在更新,说明有负环
    {
        bool update = 0;
        for(int j = 0; j < m; j++)
        {
            int x = a[j].u, y = a[j].v;
            //cout<<x<<" "<<y<<" "<<a[j].w<<endl;
            if(d[x] < INF && d[x] + a[j].w < d[y])
            {
                d[y] = d[x] + a[j].w;
                path[y] = x;
                update = 1;
                if(i == n - 1)//说明第n次还在更新
                {
                    return true;//返回真,真的存在负环
                }
            }
        }
        if(!update)
            break;//如果没更新了,说明已经松弛完毕
    }
    for(int i = 0; i < n; i++)
    {
        if(i == v0)
            continue;
        printf("从%d到%d距离是:%2d   ", v0, i, d[i]);
        stack<int>q;
        int x = i;
        while(path[x] != -1)
        {
            q.push(x);
            x = path[x];
        }
        cout<<v0;
        while(!q.empty())
        {
            cout<<"->"<<q.top();
            q.pop();
        }
        cout<<endl;
    }
    return false;
}
int main()
{
    cin >> n >> m;
    for(int i = 0; i < m; i++)
        cin >> a[i].u >> a[i].v >> a[i].w;
    if(Bellman(0))
        cout<<"存在负环"<<endl;
    else
        cout<<"不存在负环"<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值