poj 2240 汇率 floryd算法变形
来源:互联网 发布:java 多线程 csdn 编辑:程序博客网 时间:2024/06/15 18:47
Arbitrage
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 19824 Accepted: 8391
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 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
Ulm Local 1996
[Submit] [Go Back] [Status] [Discuss]
题目大意:可以简单描述为知道从i到j的汇率,问能不能赚钱
解题思路:问能不能赚钱,就是当i到i初始是 t 时,从i到i的最长路径能不能大于t,如果有这样的情况就是能赚钱
方法就是floryd算法变形 重在理解 注意变形 ,多做题才能熟练掌握~~~#include <stdio.h>#include <algorithm>#include <iostream>#include <string.h>using namespace std;#define INF 0x3f3f3f3f#define N 110double maps[N][N]; /// double 存汇率int n,m;void init(){ int i,j; for(i=0;i<=n;i++) { for(j=0;j<=n;j++) { maps[i][j]=(i==j)? 1: 0; } }}void floyd(){ int i,j,k; for(k=1;k<=n;k++) { for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(maps[i][k]*maps[k][j]>maps[i][j]) ///若汇率之积大于原来的汇率 则变换 maps[i][j]=maps[i][k]*maps[k][j]; } } }}char str[50][110];char a[110],b[110];int main(){ int i,j,ca=1,k; double x; while(scanf("%d",&n),n) { init(); for(i=1;i<=n;i++) { scanf("%s",str[i]); } scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%s %lf %s",a,&x,b); for(j=1;j<=n;j++) { if(strcmp(str[j],a)==0) ///查找a在str中的位置 break; } for(k=1;k<=n;k++) { if(strcmp(str[k],b)==0) ///查找b的位置 break; } maps[j][k]=x; ///类似最短路 路径权值~ } floyd(); printf("Case %d: ",ca++); for(i=1;i<=n;i++) { if(maps[i][i]>1) break; ///只要赚钱就跳出即可。。。 } if(i>n) printf("No\n"); else printf("Yes\n"); } return 0;}
结构体
bellman_ford
:- Source Code
#include <iostream> #include <string.h> using namespace std; #define MAXC 100 #define MAXV 50 #define MAXM 10000 typedef struct{ int a,b; double rate; }Edge; Edge edge[MAXM]; int n,m; void Input(){ char s[MAXV][MAXC],a[MAXC],b[MAXC]; int i,k,j; for(i=1;i<=n;i++) scanf("%s",s[i]); scanf("%d\n",&m); for(i=1;i<=m;i++){ scanf("%s %lf %s",a,&edge[i].rate,b); for(j=1;j<=n;j++) if(!strcmp(s[j],a)) break; for(k=1;k<=n;k++) if(!strcmp(s[k],b)) break; edge[i].a=j; edge[i].b=k; } } int bellman_ford(){ int i,j; double d[MAXV]; for(i=1;i<=n;i++) d[i]=0; d[1]=1; for(i=1;i<=n;i++){ for(j=1;j<=m;j++) if(d[edge[j].a]*edge[j].rate>d[edge[j].b]) d[edge[j].b]=d[edge[j].a]*edge[j].rate; } for(j=1;j<=m;j++) if(d[edge[j].a]*edge[j].rate>d[edge[j].b]) return 1; return 0; } int main(){ int cas=1,i; while(scanf("%d\n",&n) && n){ Input(); printf("Case %d: ",cas++); if(!bellman_ford()) printf("No\n"); else printf("Yes\n"); } return 0; }
0 0
- poj 2240 汇率 floryd算法变形
- poj2240--floryd算法变形
- 图的算法floryd
- poj 1860 汇率2
- floyd算法汇率
- POJ 3255 Roadblocks Dijkstra 算法变形
- POJ 3020 Antenna Placement 匈牙利算法变形
- 蓝桥杯 算法训练 Car的旅行路线 By Assassin [Floryd]
- POJ 2240:Arbitrage:folyd最短路算法变形求有向图的盈利环存在
- 汇率
- 变形算法
- Poj 3660 Cow Contest (传递闭包 Floyd算法变形)
- poj 1860 Currency Exchange(BellmanFord算法的变形,求正环)
- POJ 1062 昂贵的聘礼【Dijkstra算法变形】
- POJ 1860 Currency Exchange(BellmanFord算法的变形,求正环)
- POJ 2253 Frogger(最短路变形,floyd算法)
- POJ 2240 Arbitrage 简单变形的Floyd
- POJ:2240 Arbitrage(bellmanford判负环变形题)
- 二元正态分布
- 为什么在MySQL数据库中无法创建外键?(MyISAM和InnoDB详解)
- Spark 附带示例完整解释(原文已在IBM Developworks发表)
- No resource found that matches the given name
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
- poj 2240 汇率 floryd算法变形
- 机器学习之线性回归
- maven添加sqljdbc依赖
- 一些有用的函数
- TOEFL writing
- iOS 缓存清除方法
- 排序算啊之选择排序
- stack or queue
- Codeforces Round #225 (Div. 1) C-Propagating tree (DFS序+线段树/树状数组)