hdu 1217 poj 1806 最短路flord求 货币交换套利 注意3个循环的顺序
来源:互联网 发布:广工数据挖掘2016 编辑:程序博客网 时间:2024/04/28 07:48
Arbitrage
T ime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3082 Accepted Submission(s): 1399
Problem Description
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent.
Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Input
The input file will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".
Sample Input
3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFrenchFranc 0.21 USDollar3USDollarBritishPoundFrenchFranc6USDollar 0.5 BritishPoundUSDollar 4.9 FrenchFrancBritishPound 10.0 FrenchFrancBritishPound 1.99 USDollarFrenchFranc 0.09 BritishPoundFrenchFranc 0.19 USDollar0
Sample Output
Case 1: YesCase 2: No
Source
University of Ulm Local Contest 1996
Recommend
Eddy
题意 :输入n 表示有n中货币,输入m 之后m行 每行格式为 货币名 兑换比率 货币名 即 a p b 表示1单元货币a可以兑换p单位b
问这样兑换形成一个圈之后回到原来的货币,能不能赚到钱 即用1单元的货币和其他货币兑换 若干次兑换之后兑换回来原来的货币 是不是货币量大于1了
是则输出Yes
思路:
map+最短路(flord算法)
利用了flord的性质求最大值 很帅
具体看代码
:
#include<stdio.h>#include<map>#include<string>#include<string.h>using namespace std;double a[100][100];char s1[1000],s2[1000];int pos,n,flag,time,vis[50];double mmax(double a,double b){ if(a>b) return a; else return b;}int main(){ int m,i,j,k,cas=0; char s[1000]; while(scanf("%d",&n)!=EOF) { if(!n) break; cas++; map<string,int>mp; int cnt=0; for(i=0;i<n;i++) { scanf("%s",s); if(mp.find(s)==mp.end()) mp[s]=cnt++; } for(i=0;i<n;i++) for(j=0;j<n;j++) a[i][j]=0.0; scanf("%d",&m); while(m--) { int id1,id2; double mid; scanf("%s %lf %s",s1,&mid,s2); if(mp.find(s1)==mp.end()||mp.find(s2)==mp.end()) continue; id1=mp[s1]; id2=mp[s2]; a[id1][id2]=mid; // printf("a[%d][%d]=%lf\n",id1,id2,mid); } flag=0; for(int k=0;k!=n;k ++) for(int i=0;i!=n;i ++) for(int j=0;j!=n; j ++) { a[i][j]=mmax(a[i][j],a[i][k]*a[k][j]); if(a[i][j]> 1 && i==j) { flag=true; break; } } if(flag) printf("Case %d: Yes\n",cas); else printf("Case %d: No\n",cas); } return 0;}
另外一个 POJ 1860
题意 :
有一些货币交易所,每个货币交易所里只能进行两种货币的交换,现在有一定数量的某种货币,判断能否通过货币之间的交换,增加这种货币的数量。
注意 交换货币需要交纳交易费用
汇率Rab, 增加了一个手续费 Cab 。。。。。。。每次的结果是 (本金 - 手续费) * 汇率,而且有一个人拥有的钱的类型是已知的,拥有的value 钱的个数也是已知的, 问你能不能增值。
poj1860这道题,则刚好把“缩短”这个概念换为“货币能增值”,如果经过某个环,让货币增值了,那么就可以让主人公的货币一直增长下去。进行N-1次“增值”之后,如果还能继续增值,则说明图中存在能无限把货币增值的环。
转载于 http://blog.csdn.net/mazheng1989/article/details/6939237
for(int k=0;k!=n;k ++)
for(int i=0;i!=n;i ++)
for(int j=0;j!=n; j ++)
三者的顺序不要搞错 如果把k的放最后就错了 解释一下:
假设对于 a->b->c->d 这样一条路
求a b 之间的最短路 如果k循环在最后 那么 求dis[a][d] = max(dis[a][d], dis[a][c]+dis[c][d]);
但是此时dis[a][c]还不一定求出来 此时dis[a][c]还不一定是最优的 ! 所以我们要先把所有的以某一点为间接点的求完 即k循环放在最上
转载于 http://blog.csdn.net/mazheng1989/article/details/6939237
#include<iostream>#include<fstream>using namespace std;int N,M,S;int all;double V;double dis[202];struct Edge{ int v1; int v2; double rat; double com;}edges[404];bool bellmanford(){for(int i=0;i<N-1;i++){ bool flag=false; for(int j=0;j<all;j++) { int v1=edges[j].v1; int v2=edges[j].v2; if((dis[v1]-edges[j].com)*edges[j].rat>dis[v2]) { dis[v2]=(dis[v1]-edges[j].com)*edges[j].rat; flag=true; } } if(flag==false) break;}for(int j=0;j<all;j++){int v1=edges[j].v1;int v2=edges[j].v2;if((dis[v1]-edges[j].com)*edges[j].rat>dis[v2])return true;}return false;}int main(){ while(cin>>N>>M>>S>>V){all=0;for(int i=0;i<M;i++){ cin>>edges[all].v1>>edges[all].v2>>edges[all].rat>>edges[all].com; edges[all+1].v1=edges[all].v2; edges[all+1].v2=edges[all].v1; all++; cin>>edges[all].rat>>edges[all].com; all++;}for(int i=1;i<=N;i++){dis[i]=0;}dis[S]=V;if(bellmanford()){cout<<"YES"<<endl;}else cout<<"NO"<<endl;}return 0;}
for(int k=0;k!=n;k ++)
for(int i=0;i!=n;i ++)
for(int j=0;j!=n; j ++)
三者的顺序不要搞错 如果把k的放最后就错了 解释一下:
假设对于 a->b->c->d 这样一条路
求a b 之间的最短路 如果k循环在最后 那么 求dis[a][d] = max(dis[a][d], dis[a][c]+dis[c][d]);
但是此时dis[a][c]还不一定求出来 此时dis[a][c]还不一定是最优的 ! 所以我们要先把所有的以某一点为间接点的求完 即k循环放在最上
- hdu 1217 poj 1806 最短路flord求 货币交换套利 注意3个循环的顺序
- POJ 1847 tram 最短路 (Flord)
- hdu 1217 套利 (floyd多源最短路)
- poj 1125 (求起点的最短路)
- poj1860 Currency Exchange(货币交换,最短路问题)
- poj 3463/hdu 1688 求次短路和最短路个数
- POJ 3615 floyd 求任意起点终点的最短路
- poj 1602 昂贵的聘礼(dijkstra求最短路)
- HDU 1217 Arbitrage 货币交换(floyd)
- hdu:4360(spfa)求各种状态的最短路
- hdu 4460 求所有任意两点间的最短路
- hdu 2066 一个人的旅行(Dijkstra求最短路问题)
- hdu 2066 一个人的旅行(Dijkstra求最短路)
- Arbitrage 最短路径的变形 flord
- [HDU 1548] SPFA 最短路权值设为1, 然后求a--b的最短路
- hdu 2544 最短路 (diskstra求最短路)
- poj 3463 Sightseeing(dij求最短路+次短路(比最短路长1)的总走法)
- hdu 1217 # 最短路
- Daily-C-Study(24):C语言内存管理
- BUAA集训队选拔赛部分简单题解
- 卡特兰数(Catalan)
- Norlit OS —— 自制操作系统 第0章 热身运动
- 编程基础-----qsort函数排序
- hdu 1217 poj 1806 最短路flord求 货币交换套利 注意3个循环的顺序
- (泛化,实现,依赖,关联(聚合,组合))
- applicationContext.xml报错问题
- 苹果所有常用证书,appID,Provisioning Profiles配置说明及制作图文教程(精)
- Embedded Linux Primer----嵌入式Linux基础教程--2.3.1~4节--存储注意事项
- iso loadview死循环分析
- 收件箱的提示为啥总没有
- 排序算法总结
- POJ 3368(Frequent values)