Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 13800   Accepted: 5815

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 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

  1. 3
  2. USDollar
  3. BritishPound
  4. FrenchFranc
  5. 3
  6. USDollar 0.5 BritishPound
  7. BritishPound 10.0 FrenchFranc
  8. FrenchFranc 0.21 USDollar
  9.  
  10. 3
  11. USDollar
  12. BritishPound
  13. FrenchFranc
  14. 6
  15. USDollar 0.5 BritishPound
  16. USDollar 4.9 FrenchFranc
  17. BritishPound 10.0 FrenchFranc
  18. BritishPound 1.99 USDollar
  19. FrenchFranc 0.09 BritishPound
  20. FrenchFranc 0.19 USDollar
  21.  
  22. 0

Sample Output

  1. Case 1: Yes
  2. Case 2: No

Source

  1. //766 MS 772 KB GNU C++
  2. /*
  3.  
  4. 题意:
  5. 给出一个图,n个点,m条边,每条边有一个权值,求是否存在一条回路,使边的权值积大于1
  6.  
  7. 最短路径:
  8. floyd小变异。数据比较小,直接用floyd遍历一遍,然后判断是否存在可行解
  9.  
  10. */
  11. #include<iostream>
  12. #include<map>
  13. #include<string>
  14. #include<stdio.h>
  15. using namespace std;
  16. double g[][];
  17. int n,m;
  18. void floyd()
  19. {
  20. for(int k=;k<=n;k++)
  21. for(int i=;i<=n;i++)
  22. for(int j=;j<=n;j++)
  23. if(g[i][k]!=- && g[k][j]!=-)
  24. if(g[i][j]==- || g[i][k]*g[k][j]>g[i][j])
  25. g[i][j]=g[i][k]*g[k][j];
  26. int flag=;
  27. for(int i=;i<=n;i++){
  28. //printf("%lf\n",g[i][i]);
  29. if(g[i][i]>1.0)
  30. flag=;
  31. }
  32. if(flag) puts("Yes");
  33. else puts("No");
  34. }
  35. int main(void)
  36. {
  37. string a,b;
  38. double c;
  39. int k=;
  40. while(cin>>n,n)
  41. {
  42. for(int i=;i<=n;i++)
  43. for(int j=;j<=n;j++)
  44. g[i][j]=-;
  45. map<string,int>M;
  46. for(int i=;i<=n;i++){
  47. cin>>a;
  48. M[a]=i;
  49. }
  50. scanf("%d",&m);
  51. for(int i=;i<m;i++){
  52. cin>>a>>c>>b;
  53. g[M[a]][M[b]]=c;
  54. }
  55. printf("Case %d: ",k++);
  56. floyd();
  57. }
  58. return ;
  59. }

poj 2240 Arbitrage (最短路径)的更多相关文章

  1. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

  2. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  3. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  4. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  5. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. POJ 2240 Arbitrage(floyd)

    http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...

  7. poj 2240 Arbitrage (最短路 bellman_ford)

    题目:http://poj.org/problem?id=2240 题意:给定n个货币名称,给m个货币之间的汇率,求会不会增加 和1860差不多,求有没有正环 刚开始没对,不知道为什么用 double ...

  8. POJ 2240 Arbitrage(判正环)

    http://poj.org/problem?id=2240 题意:货币兑换,判断最否是否能获利. 思路:又是货币兑换题,Belloman-ford和floyd算法都可以的. #include< ...

  9. poj 2240 Arbitrage(Bellman_ford变形)

    题目链接:http://poj.org/problem?id=2240 题目就是要通过还钱涨自己的本钱最后还能换回到自己原来的钱种. 就是判一下有没有负环那么就直接用bellman_ford来判断有没 ...

随机推荐

  1. NestedScrollView和RecyclerView使用,并设置间距

    NestedScrollView和RecyclerView使用,并设置间距: 效果图如下: 1.NestedScrollView 和RecyclerView嵌套问题(类似ScrollView 和lis ...

  2. React Native ref高级用法&&setNativeProps使用

    ref属性不只是string ref属性不仅接受string类型的参数,而且它还接受一个function作为 callback.这一特性让开发者对ref的使用更加灵活. render() { retu ...

  3. js实现二分查找

    二分查找需要数组是有序的,1.先从有序数组的最中间元素开始查找,如果和要查找的元素相等,直接返回索引,若不相等则下一步.2.如果指定的元素大于或者小于中间元素,则在大于或小于的那一半区域内查找,重复第 ...

  4. 【学时总结】◆学时·VII◆ 高维DP

    ◆学时·VII◆ 高维DP 自学之余,偶遇DP…… ◇ 算法概述 顾名思义——一种处理多方面状态的DP,这种DP特点是……每一维的大小都不算太大(不然用dp数组存储下来内存会炸),而且枚举时容易超时… ...

  5. LeetCode977. 有序数组的平方

    问题:977. 有序数组的平方 给定一个按非递减顺序排序的整数数组 A,返回每个数字的平方组成的新数组,要求也按非递减顺序排序. 示例 1: 输入:[-4,-1,0,3,10] 输出:[0,1,9,1 ...

  6. mysql_old_wrong

    DELIMITER $ create trigger auto_post_person_pointafter insert on post for each rowbeginupdate person ...

  7. python——字符串的操作判断

    s为字符串 s.isalnum()  所有字符都是数字或者字母,为真返回 Ture,否则返回 False. s.isalpha()   所有字符都是字母,为真返回 Ture,否则返回 False. s ...

  8. sql查询平均下单时间

    SQL查询订单平均审核时长 今天在写一个sql,需求是算一个订单在执行状态中的各个节点的时长 比如在订单中,状态0为开始接单,状态3为已经审核,那么现在需要计算每个客服的平均审核时长 像图中所示:这个 ...

  9. Android面试收集录 Android布局

    1.请说出Android中的五种布局,并介绍作用? FrameLayout(堆栈布局),层叠方式显示,类似于PhotoShop上的层叠图层. LinearLayout(线性布局),将视图以水平或者垂直 ...

  10. linux socket下send()&recv()调用

    1.send 函数 int send( SOCKET s, const char FAR *buf, int len, int flags ); 不论是客户还是服务器应用程序都用send函数来向TCP ...