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

优化后复杂度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!

最新文章

  1. Django 1.7 Tutorial 学习笔记
  2. SQLite中的时间日期函数(转)
  3. 【代码笔记】iOS-带输入框的UIAlertView
  4. 【openGL】画圆
  5. Sample Join Analysis
  6. iOS - Swift NSLocale 本地化信息
  7. bzoj3995[SDOI2015]道路修建
  8. (原)ubuntu16中安装moses
  9. win10安装数据库:[INS-13001]
  10. ExcelHelper Excel,Export,Import
  11. linux下crontab的使用实现
  12. 在 ASP.NET Core 中使用 SignalR
  13. ng-options指令语法
  14. 1692: [Usaco2007 Dec]队列变换|后缀数组|贪心
  15. 美国康奈尔大学BioNB441元胞自动机MATLAB应用
  16. servlet篇 之 跳转问题
  17. 微软develop apps在QQ上部分功能的实现
  18. SQL server 获得 表的主键,自增键
  19. 【C#】使用bat文件安装卸载Window服务
  20. MVC 自定义路由

热门文章

  1. Cocos结构
  2. Web前端错题模糊题记录
  3. HTML基础——网站后台显示页面
  4. 手工清理win7系统C盘的技巧
  5. git pull 失败 ,提示:fatal: refusing to merge unrelated histories
  6. plsql 查询历史执行语句
  7. 使用Python进行多线程检查.moe三位剩余有效域名
  8. How Javascript works (Javascript工作原理) (四) 事件循环及异步编程的出现和 5 种更好的 async/await 编程方式
  9. Vue学习之v-if与v-show的区别
  10. css 遮照镂空效果