Bellman-Ford算法
来源:互联网 发布:g.719协议源码 编辑:程序博客网 时间:2024/05/09 09:08
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;const int maxn = 100000 + 5;const int INF = 99999999;int d[maxn], cnt[maxn], n, m; //n,m分别是定点数目以及边数bool inq[maxn];struct Edge { int from, to, dist; Edge(int u, int v, int d) : from(u), to(v), dist(d) {}};vector<Edge> edges;vector<int> G[maxn];bool bellman_ford(int s) { queue<int> q; memset(cnt, 0, sizeof(cnt)); memset(inq, 0, sizeof(inq)); for(int i = 0; i < n; i++) d[i] = INF; d[s] = 0; inq[s] = true; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); inq[u] = false; for(int i = 0; i < G[u].size(); i++) { Edge& e = edges[G[u][i]]; if(d[u] < INF && d[e.to] > d[u] + e.dist) { d[e.to] = d[u] + e.dist; p[e.to] = G[u][i]; if(!inq[e.to]) { q.push(e.to); inq[e.to] = true; if(++cnt[e.to] > n) return false; //判断是否含有负环 } } } } return true;}int main(){ return 0;}
0 0
- Bellman-Ford&SPFA算法
- Bellman-ford算法实现
- Bellman-Ford 算法详解
- Bellman-Ford算法
- Bellman-Ford算法
- Bellman-Ford路由算法
- Bellman-Ford算法分析
- bellman ford算法
- Bellman-Ford算法
- Bellman-Ford算法总结
- Bellman-ford算法
- bellman-ford算法
- bellman-ford 算法
- Bellman-Ford算法
- Bellman-Ford算法
- bellman ford 算法
- Bellman-Ford 算法
- Bellman-Ford算法
- 解决Android中No resource found that matches android:TextAppearance.Material.Widget.Button.Inverse问题
- 重载赋值运算符
- lesson3_VC++对windows程序简单封装
- POJ-2553 The Bottom of a Graph (强连通分量[Tarjan])
- leetcode-24. Swap Nodes in Pairs
- Bellman-Ford算法
- C++循环类型
- hdu 1159 && POJ 1458Common Subsequence(LCS)
- 重载算术运算符与重载赋值运算符
- 利用递归解决汉诺塔问题
- HttpUrlConnection以GET方式和POST方式请求服务器
- floyd算法
- 再说c++虚析构函数
- Java学习笔记