poj1860 Bellman-Ford
来源:互联网 发布:类似于1942的电影知乎 编辑:程序博客网 时间:2024/05/05 14:15
一开始是知道这道题用的是贝尔曼福特最短路径算法,但是稀里糊涂地看了一点算法导论就着手开始做果然还是不行啊。
wa了一次之后决心好好研究一下这个算法,第二天还到YouTube上找了视频来看,果然清晰很多。
认真改完代码以后ac了,我可能等学了另一种算法之后再回来继续刷这道题,现在我还是喜欢看到解题数量往上涨的成就感啊。
所以这次也会认真地分析一下这道题,如果有可能的话,写一个详细一点的解题报告。
先贴ac代码:
#include <iostream>#include <vector>using namespace std;vector<vector<double>> rate;vector<vector<double>> commission;vector<double> money;int num_currencies,exchange_points,no_Nick;double quantity;void relax(int v, int u){ if(money[v]<(money[u]-commission[u][v])*rate[u][v]&&money[u]>commission[u][v]){ money[v]=(money[u]-commission[u][v])*rate[u][v]; }}bool judge(){ for(int i=0;i<num_currencies-1;i++){ for(int j=0;j<num_currencies;j++){ for(int m=0;m<num_currencies;m++){ if(m==j||rate[j][m]==-1) continue; relax(m,j); } } } for(int j=0;j<num_currencies;j++){ for(int m=0;m<num_currencies;m++){ if(m==j||rate[j][m]==-1) continue; if(money[m]<(money[j]-commission[j][m])*rate[j][m]&&money[j]>commission[j][m]){ cout<<"YES"<<endl; return true; } } } cout<<"NO"<<endl; return false; }void search_table(){ }void fill_table(){ for(int i=0;i<exchange_points;i++){ int A,B; double Rab,Cab,Rba,Cba; cin>>A>>B>>Rab>>Cab>>Rba>>Cba; rate[A-1][B-1]=Rab; rate[B-1][A-1]=Rba; commission[A-1][B-1]=Cab; commission[B-1][A-1]=Cba; }}int main(int argc, const char * argv[]) { // insert code here... cin>>num_currencies>>exchange_points>>no_Nick>>quantity; for(int i=0;i<num_currencies;i++){ money.push_back(0); } money[no_Nick-1]=quantity; for(int i=0;i<num_currencies;i++){ vector<double> v(num_currencies); rate.push_back(v); for(int j=0;j<num_currencies;j++){ rate[i][j]=-1; } } for(int i=0;i<num_currencies;i++){ vector<double> v(num_currencies); commission.push_back(v); for(int j=0;j<num_currencies;j++){ commission[i][j]=-1; } } fill_table(); judge(); return 0;}
首先是关于这道oj,汇率问题貌似是oj中的一类问题。做oj中的汇率就要摒弃常识中对汇率的理解,oj中的汇率就简单看成是简单的权向图中的每一条边的weight,相互交换就是双向的边,也可以看成是一个小型的环状结构。
能够盈利的条件是存在一个负环,这样可以在里面转无数圈再回到原来的货币一定是盈利的。
问题就出在如何判断所有的结点的值不会再变了,这是我在看Bellman-Ford之前最困惑的问题。
而Bellman-Ford的算法给了一个保证就是在松弛每条边|V|-1次之后,负环影响之外的结点值就是稳定的了。
剩下最后一次,再把每一条边走过一遍,如果还有值的变动,说明有负环存在。
0 0
- poj1860 Bellman-Ford
- poj1860(bellman-ford)
- poj1860 Bellman-Ford
- POJ1860(Bellman-Ford思想)
- poj1860 bellman ford
- poj1860(bellman ford 算法)
- POJ1860—Bellman-Ford算法
- poj1860 Currency Exchange --- Bellman-Ford
- poj1860 Bellman-ford算法应用
- poj1860(图论,bellman-ford)
- Poj1860 Currency Exchange Bellman-Ford
- poj1860之Bellman-Ford解法
- POJ1860《Currency Exchange》方法:反向Bellman-ford
- POJ1860 换零钱套利 图论(Bellman-ford)
- POJ1860 Currency Exchange(Bellman-ford的变形)
- POJ1860 Currency Exchange 反向bellman-ford
- POJ1860 Currency Exchange 最短路bellman-ford
- Bellman-ford变形 poj1860 Currency Exchange
- 小马哥----高仿小米4 tc01刷机拆机主板图与开机界面图 分联通版与移动版
- 黑马程序员——C语言基础01—C语言概述
- [leetcode-303]Range Sum Query - Immutable
- 【HDU】2966 In case of failure【KD树】
- 一些小方法的集合
- poj1860 Bellman-Ford
- ZigBee相关资源
- 深圳地铁规划图2
- ViewPager不为人知的秘密
- 黑马程序员_继承
- SQL 替换指定字段某个位置的值
- 老程序员应该记住的 5 件事
- 自定义控件--自定义内容可以滚动的TextView
- LightOJ 1030 - Discovering Gold(dp)