博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 1217 Arbitrage(Floyd)
阅读量:5297 次
发布时间:2019-06-14

本文共 4017 字,大约阅读时间需要 13 分钟。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217

Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2956    Accepted Submission(s): 1333


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.
 

 

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.
 
 

 

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
3 USDollar BritishPound FrenchFranc 3 USDollar 0.5 BritishPound BritishPound 10.0 FrenchFranc FrenchFranc 0.21 USDollar 3 USDollar BritishPound FrenchFranc 6 USDollar 0.5 BritishPound USDollar 4.9 FrenchFranc BritishPound 10.0 FrenchFranc BritishPound 1.99 USDollar FrenchFranc 0.09 BritishPound FrenchFranc 0.19 USDollar 0
 

 

Sample Output
Case 1: Yes Case 2: No
 

 

Source
 

 

Recommend
Eddy
 

一开始看不出什么所以然来(最近一段时间没做题的缘故),后来看数据不大,想用搜索做,但是这貌似不太符合,果然提交后TLE了,之后想想,可以先用Floyd或Dijkstra处理下,然后再任意将两个相对的点对(这里理解为点对,把一种币看作一个点,与另一种币的汇率作为两个点的距离,相对的点对即相反的点对,如(0,1)和(1,0))相乘,如果结果大于1.0,说明符合要求,输出Yes,否则输出No,如,美元换港币的汇率是1.5,港币换美元的汇率是0.7,显然,二者之积大于1.0,因此输出Yes。

注意最后输出的格式,"Case case:"后面有空格。。。。。

 

贴下代码:

1 #include
2 #include
3 #include
4 #include
5 #define exp 1e-9 6 char mo[50][50]; 7 double g[50][50]; 8 int vis[50]; 9 int n, ok;10 int finds(char s[])11 {12 int i;13 for(i = 0; i < n; i++)14 if(strcmp(mo[i], s) == 0)return i;15 }16 void getdata()17 {18 int i, a, b, m;19 char s1[100], s2[100];20 double rate;21 for(i = 0; i < n; i++)22 scanf("%s", mo[i]);23 scanf("%d", &m);24 getchar();25 memset(g, 0, sizeof(g));26 while(m--)27 {28 scanf("%s %lf %s", s1, &rate, s2);29 a = finds(s1);30 b = finds(s2);31 g[a][b] = rate;32 }33 }34 void floyd()35 {36 int i, j, k;37 for(k = 0; k < n; k++)38 for(i = 0; i < n; i++)39 for(j = 0; j < n; j++)40 {41 if(g[i][j] < g[i][k]*g[k][j])42 g[i][j] = g[i][k] * g[k][j];43 }44 }45 void check()46 {47 int i, j;48 if(n == 1 && g[0][0] > 1.0)49 {50 printf("Yes\n");51 return ;52 }53 for(i = 0; i < n; i++)54 for(j = 0; j < n; j++)55 {56 if(i == j)continue;57 if(g[i][j]*g[j][i] > 1.0)58 {59 printf("Yes\n");60 return ;61 }62 }63 printf("No\n");64 }65 int main()66 {67 int cas = 0;68 while(scanf("%d", &n) != EOF && n)69 {70 getchar();71 getdata();72 printf("Case %d: ", ++cas);73 floyd();74 check();75 }76 return 0;77 }

 

转载于:https://www.cnblogs.com/lfeng/archive/2013/04/26/3045769.html

你可能感兴趣的文章
Visual studio之C# 调用系统软键盘(外部"osk.exe")
查看>>
移动应用开发选型:向左还是向右?
查看>>
开发进度一
查看>>
十天冲刺(6)
查看>>
MyBaits学习
查看>>
MySQL安装的详细步骤
查看>>
管道,数据共享,进程池
查看>>
Java基础--面向对象编程4(多态)
查看>>
CSS
查看>>
shell 管道和tee使用时获取前面命令返回值
查看>>
[LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming
查看>>
[Cypress] Stub a Post Request for Successful Form Submission with Cypress
查看>>
WordPress GRAND FlAGallery插件“s”跨站脚本漏洞
查看>>
程序集的混淆及签名
查看>>
MATLAB中subplot的用法
查看>>
thinkphp框架 中 ajax 的应用
查看>>
JAVA排序(一) Comparable接口
查看>>
iTerm2 + Oh My Zsh
查看>>
判断9X9数组是否是数独的java代码
查看>>
ExtJS学习之路第一步:对比jQuery,认识ExtJS
查看>>