Bellman-Ford判断有负环的算法模板

<p class="p1">
</p>
#include<cstring>
#include<iostream>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int int_max = 0x07777777;
const int int_min = 0x80000000;
const int maxn = 1000;
struct Edge {
    int to, next,dist;
}es[410*410*2];
int head[maxn], vis[maxn],ccnt;
double d[maxn];
int n,m;
int cnt[maxn];
void addedge (int from, int to, int dist){
    es[ccnt].to = to;
    es[ccnt].dist = dist;
    es[ccnt].next = head[from];
    head[from] = ccnt++;
}
bool negativeCycle (){
    queue<int> q;
    int limit=(int)sqrt(1.0*(n+m));
    memset(vis, 0, sizeof(vis));
    memset(cnt, 0, sizeof(cnt));
    for(int i = 1; i <= n+m; i++) {d[i] = 0; vis[i] = true; q.push(i);}
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int i = head[u]; i!=-1; i=es[i].next){
            if(d[es[i].to] > d[u]+es[i].dist){
                d[es[i].to] = d[u]+es[i].dist;
                if(!vis[es[i].to]){
                    q.push(es[i].to);
                    vis[es[i].to] = true;
                    if(++cnt[es[i].to] > limit) return true;
                }
            }
        }
    }
    return false;
}



#include<cstdio>
#include<cstring>
#include<iostream>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int int_max = 0x07777777;
const int int_min = 0x80000000;
const int maxn = 50005;
struct Edge {
    int to, dist,next;
}es[maxn];
int head[maxn],d[maxn],vis[maxn],cnt;

void addedge (int from, int to, int dist){
    es[cnt].to = to;
    es[cnt].dist = dist;
    es[cnt].next = head[from];
    head[from] = cnt++;
}
int n,s,t;
void spfa (){
    for(int i = s; i <= t; i++) d[i] = int_max;
    memset(vis, 0, sizeof(vis));
    d[s] = 0;
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for(int i = head[u]; i != -1; i = es[i].next){
            if(d[es[i].to] > d[u]+es[i].dist){
                d[es[i].to] = d[u]+es[i].dist;
                if(!vis[es[i].to]){
                    q.push(es[i].to);
                    vis[es[i].to] = 1;
                }
            }
        }
    }
}


应用:

1,给定一个n个点,m条边的加权有向图,求平均权值最小的回路。

分析:使用二分法求解。对于一个猜测值mid,只需要判断是否存在一个平均值小于mid的回路。如何判断呢?假设存在一个包含k条边的回路,回路上各条边的权值为w1,w2,w3,....wk,那么平均值小于mid意味着w1+w2+w3+...+wk<k*mid,即:(w1-mid)+(w2-mid)+(w3-mid)+...+(wk-mid)<0。换句话说,只要把每条边(a,b)的权值变成w(a,b)-mid,再判断新图中是否有负权回路即可。

bool test (double x){
    for(int i = 0; i < es.size(); i++){
        es[i].dist -= x;
    }
    bool ret = negativeCycle();
    for(int i = 0; i < es.size(); i++){
        es[i].dist += x;
    }
    return ret;
}

int main (){
    double ub; //用来表示初始化之后边中权值最大值。
    if(!test(ub+1)) printf("No cycle found.\n");
    else{
        double L = 0;
        double R = ub;
        while(R-L > 1e-3){
            double M = L + (R-L)/2;
            if(test(M)) R = M; else L = M;
        }
        printf("%.2lf\n",L);
    }
    return 0;
}



















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值