用于求可带负权单源有向图

优化后复杂度O(nm)

如果图中存在负环,就不存在最小路

这种情况下,就一定会有一个顶点被松弛多于n-1次,Bellman-Ford可直接判断出来

我在网上看到SPFA,发现就是优化后的Bellman-Ford算法,没什么特别的

常见有三种版本的BellmanFord:原版,队列优化Bellman,栈优化Bellman

看起来栈优化的Bellman比较快速?不存在负权下,直接用Dijskra即可

模板

// Bellman-Ford
// to check negtive circle, if not exists return minumum distance
//
// Description:
// 1. do n-1 times relax for all edges (or check if dis[u]=dis[v]+dis[v, u])
//
// Details:
// 1. use queue to push and get the vertax
// 2. use cnt[verIdx] to count n-1 for each vertax
// 3. use inq[verIdx] to check if in queue(when process u, set inq[u] false)
// 4. despite of inq, do relax(but que.push)
// 5. initialize edge, G, dis and que #include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn=205, INF=0x3f3f3f3f;
struct Edge{
int from, to, dis;
Edge(int from=0, int to=0, int dist=0):
from(from), to(to), dist(dist) {}
};
vector<Edge> edge;
vector<int> G[maxn+5];
int n;
void AddEdge(int from, int to, int val){
edge.push_back(Edge(from, to, val));
G[from].push_back(edge.size()-1);
edge.push_back(Edge(to, from, val));
G[to].push_back(edge.size()-1);
} int BellmanFord(int st){
int ans=0, cnt[maxn+5]={0}, dist[maxn+5];
bool inq[VerMax]={0};
memset(dist, INF, sizeof(dist));
queue<int> que;
que.push(st);
inq[st]=true; dist[st]=0;
while (que.size()){
int from=que.front(); que.pop();
inq[from]=false;
for (int i=0; i<G[from].size(); i++){
Edge &e=edge[G[from][i]]; int to=e.to;
if (dis[to]<=dis[from]+e.dis) continue;
dis[to]=dis[from]+e.dis;
if (inq[to]) continue;
que.push(to); inq[to]=true;
// if (++cnt[to]>verSize) return -1;
// why n+1? (this code from purple book P363)
if (++cnt[to]>n-1) return -1;
}
}
return ans;
}

注意

  1. 需初始化dist为INF,inq为false,cnt为0;还有edge, G
  2. 不要忘了inq, cnt数组
  3. 考虑dist[k]==INF,为不存在路径

例题

二进制状态,隐式图搜索

UVA-658 It's not a Bug, it's a Feature!

[笔记-图论]Bellman-Ford的更多相关文章

  1. Bellman - Ford 算法解决最短路径问题

    Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...

  2. ACM/ICPC 之 最短路径-Bellman Ford范例(POJ1556-POJ2240)

    两道Bellman Ford解最短路的范例,Bellman Ford只是一种最短路的方法,两道都可以用dijkstra, SPFA做. Bellman Ford解法是将每条边遍历一次,遍历一次所有边可 ...

  3. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

  4. uva 558 - Wormholes(Bellman Ford判断负环)

    题目链接:558 - Wormholes 题目大意:给出n和m,表示有n个点,然后给出m条边,然后判断给出的有向图中是否存在负环. 解题思路:利用Bellman Ford算法,若进行第n次松弛时,还能 ...

  5. Bellman—Ford算法思想

    ---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...

  6. Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】

    题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 poj2387 Description Bessie is out in the field and ...

  7. 图论算法——最短路径Dijkstra,Floyd,Bellman Ford

    算法名称 适用范围 算法过程 Dijkstra 无负权 从s开始,选择尚未完成的点中,distance最小的点,对其所有边进行松弛:直到所有结点都已完成 Bellman-Ford 可用有负权 依次对所 ...

  8. POJ 2240 Arbitrage (Bellman Ford判正环)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:27167   Accepted: 11440 Descri ...

  9. poj1860 兑换货币(bellman ford判断正环)

    传送门:点击打开链接 题目大意:一个城市有n种货币,m个货币交换点,你有v的钱,每个交换点只能交换两种货币,(A换B或者B换A),每一次交换都有独特的汇率和手续费,问你存不存在一种换法使原来的钱更多. ...

随机推荐

  1. 133.throw机制 抛出类类型

    #include <iostream> using namespace std; //try尝试执行,抛出throw,throw之后语句不再执行 //catch处理throw的异常 voi ...

  2. .NET框架详解

    .NET框架的战略目标 .NET框架的战略目标是在任何时候(When),任何地方(Where),使用任何工具(What)都能通过.NET的服务获得网络上的任何信息,享受网络带给人们的便捷和快乐! .N ...

  3. Python内置数据结构之列表list

    1. Python的数据类型简介 数据结构是以某种方式(如通过编号)组合起来的数据元素(如数.字符乃至其他数据结构)集合.在Python中,最基本的数据结构为序列(sequence). Python内 ...

  4. python继承 super()

    写这篇博文,始于以下问题的探究: #coding:utf-8 class A(object): def __init__(self): print 'enter A' print 'leave A' ...

  5. xampp、phpstudy安装phalcon

    1.下载扩展 https://github.com/phalcon/cphalcon/releases/tag/v3.4.1选择PHP对应版本的phalcon扩展 2.PHP.ini 配置phalco ...

  6. BZOJ 3790 神奇项链(回文自动机+线段树优化DP)

    我们预处理出来以i为结尾的最长回文后缀(回文自动机的构建过程中就可以求出)然后就是一个区间覆盖,因为我懒得写贪心,就写了线段树优化的DP. #include<iostream> #incl ...

  7. The Karplus-Strong Algorithm

    本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/48730857 Karplus-Stro ...

  8. filezilla server配置为 passive mode

    首先要配置filezilla的setting里面的Passive mode setting选项 (2)关键部分,打开win8.1下的防火墙,新建入站规则 注意,要打开80,443端口.已经passiv ...

  9. flume 读取kafka 数据

    本文介绍flume读取kafka数据的方法 代码: /************************************************************************* ...

  10. 黑马程序猿-----Java之你不得不知道的排序

    ------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...