poj 1860 Currency Exchange (SPFA)
来源:互联网 发布:js处理json 编辑:程序博客网 时间:2024/06/04 19:41
终于真正明白dijkstra算法和Bellman-Ford算法了。
Bellman-Ford算法的优势在于图中权值可以为负,缺点就是复杂度太高,但可以用队列优化,优化后就是SPFA算法了。并且可以判定图中是否有负环。
Bellman-Ford算法判定负环就是看在执行完V-1(V为图中点的个数)次松弛操作后能否再进行松弛操作,如果可以的话必定有负环,因为无负环图在进行完V-1次松弛操作后必定为最短路径,只有有负环图可以无限次松弛下去。
SPFA算法判定负环就是看是否有点被加入队列超过V次,如果有的话必定有负环,原理同上。
此题是判定图中负环的改进,判定的是“正环”,并且初始化和路径计算也与普通的不同,考察对算法的理解程度。
我用的是SPFA算法,AC代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>using namespace std;const int maxn = 1000;int n,m,s;double t;int src;vector<pair<int ,pair<double,double> > > g[maxn + 10];double dist[maxn + 10];bool inQue[maxn + 10];int num[maxn + 10];queue<int> que;double all;void spfa(){ int f = 0; for (int i=0;i<=n ;i++ ) dist[i] = 0; memset(num,0,sizeof(num)); dist[src] = all; while(!que.empty()) que.pop(); que.push(src); inQue[src] = true; while(!que.empty()){ int u = que.front(); num[u]++; if(num[u]>=n) break; que.pop(); for (int i=0;i<g[u].size() ;i++ ) if((dist[u]-g[u][i].second.second) * g[u][i].second.first > dist[g[u][i].first]){ dist[g[u][i].first] = (dist[u]-g[u][i].second.second) * g[u][i].second.first; if(!inQue[g[u][i].first]){ inQue[g[u][i].first] = true; que.push(g[u][i].first); } } inQue[u] = false; } //cout<<dist[1]<<" "<<dist[2]<<" "<<dist[3]<<endl; if(que.empty()) puts("NO"); else puts("YES");}void slove(){ scanf("%d%d%lf",&m,&src,&all); int x,y; double l,r; for (int i=0;i<m ;i++ ) { scanf("%d%d%lf%lf",&x,&y,&l,&r); g[x].push_back(make_pair(y,make_pair(l,r))); scanf("%lf%lf",&l,&r); g[y].push_back(make_pair(x,make_pair(l,r))); } spfa();}int main(){#ifdef ARTHUR_YANGfreopen("in.txt","r",stdin);#endif // ARTHUR_YANG while(~scanf("%d",&n)) { slove(); }}第一次用vector<pair<int ,pair<double,double> > >这样的数据结构,感觉爽爆了!!!有种大神的即视感。
0 0
- poj 1860 Currency Exchange (SPFA)
- poj 1860 Currency Exchange(SPFA)
- POJ 1860 Currency Exchange(spfa)
- Currency Exchange【POJ--1860】【SPFA】
- POJ 1860 Currency Exchange spfa
- Poj 1860 Currency Exchange【SPFA】
- Poj 1860 Currency Exchange ( SPFA
- [POJ 1860] Currency Exchange [spfa]
- poj 1860 Currency Exchange (spfa)
- poj 1860 Currency Exchange (最短路变形-spfa)
- POJ 1860 & ZOJ 1544 Currency Exchange(最短路SPFA)
- POJ 1860 Currency Exchange 最短路 BF & SPFA(bfs)
- POJ 1860 Currency Exchange(SPFA 判断有无“正”环)
- POJ 1860:Currency Exchange (spfa 判环)
- 文章标题 POJ 1860 : Currency Exchange(最短路--spfa)
- poj 1860 Currency Exchange (spfa或Bellman-Ford)
- POJ 1860 Currency Exchange(最短路SPFA)
- POJ-1860-Currency Exchange-寻找正权环-spfa
- android手势操作滑动效果触摸屏事件处理
- C语言深度解剖读书笔记
- POJ 3009 图的遍历+DFS+回溯
- oracle vm virtualbox按照Ubuntu
- 【转载】判断整数序列是不是二元查找树的后序遍历结果
- poj 1860 Currency Exchange (SPFA)
- 安装VirtualBox
- am335x修改sd卡cd管脚
- 动态规划 最长公共子序列
- 前端工程师必须掌握的知识点
- 并发控制与事务隔离
- C++中Sort函数使用方法
- android 实战练习 回归手机卫士 一 主界面布局
- 特殊的二进制数