找负环 bellman ford 算法

本文深入探讨了Bellman-Ford算法在解决图中存在负权边时寻找负环的问题。通过迭代松弛操作,算法可以检测出负权回路并计算最短路径。同时,文章介绍了算法的基本步骤、复杂性和应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include <sstream>
#include <ostream>
#include <algorithm>
#include <ctype.h>
#include <cmath>
#include <queue>
#include <vector>
#define inf 1e9+7
#define pi acos(-1)
#define natrule exp(1)
using namespace std;
struct edge{
    int to,from,weight;
};
int d[1005];
edge eg[1005];
bool find_negtive_loop(int m, int n){
    memset(d,0,sizeof(d));//随意设置一个除了inf之外的值
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            edge e=eg[i];
            if(d[e.to]>d[e.from]+e.weight){
                d[e.to]=d[e.from]+e.weight;
                if(i==n-1) return true;
            }
        }
    }
    return false;
}
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(75, 209, 87);"><span style="font-variant-ligatures: no-common-ligatures">//dij prim </span><span style="line-height: normal; font-family: 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures;">堆优化</span><span style="font-variant-ligatures: no-common-ligatures"> </span><span style="line-height: normal; font-family: 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures;">的</span><span style="font-variant-ligatures: no-common-ligatures"> egde</span><span style="line-height: normal; font-family: 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures;">类型不同于</span><span style="font-variant-ligatures: no-common-ligatures"> kruskal </span><span style="line-height: normal; font-family: 'Heiti SC Light'; font-variant-ligatures: no-common-ligatures;">还有</span><span style="font-variant-ligatures: no-common-ligatures">bellman —— ford</span></p>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值