Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17360   Accepted: 7308

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
    • Source Code
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. char str[40][40],str2[40],str1[40];
  7. double book[40][40];
  8. int main(){
  9. int t;
  10. int count=0;
  11. while(scanf("%d",&t)!=EOF){
  12. if(t==0)
  13. break;
  14. memset(str,0,sizeof(str));
  15. count++;
  16.  
  17. memset(book,0,sizeof(book));
  18. getchar();
  19. for(int i=0;i<t;i++){
  20. scanf("%s",str[i]);
  21. book[i][i]=1.0;
  22. getchar();
  23. }
  24. int n;
  25. scanf("%d",&n);
  26. double d;
  27. for(int i=0;i<n;i++){
  28. memset(str1,0,sizeof(str1));
  29. memset(str2,0,sizeof(str2));
  30. scanf("%s %lf %s",str1,&d,str2);
  31. int temp1,temp2;
  32. for(int ii=0;ii<t;ii++){
  33. if(strcmp(str1,str[ii])==0)
  34. temp1=ii;
  35. }
  36. for(int ii=0;ii<t;ii++){
  37. if(strcmp(str2,str[ii])==0)
  38. temp2=ii;
  39. }
  40. book[temp1][temp2]=d;
  41. getchar();
  42. }
  43.  
  44. for(int k=0;k<t;k++){
  45. for(int i=0;i<t;i++){
  46. for(int j=0;j<t;j++){
  47. if(book[i][j]<book[i][k]*book[k][j])
  48. book[i][j]=book[i][k]*book[k][j];
  49. }
  50. }
  51. }
  52.  
  53. int flag=0;
  54. for(int i=0;i<t;i++){
  55. if(book[i][i]>1.0)
  56. flag=1;
  57. }
  58. if(flag==1)
  59. printf("Case %d: Yes\n",count);
  60. else
  61. printf("Case %d: No\n",count);
  62.  
  63. }
  64. return 0;
  65. }
  1.  

poj2240最短路 floyd的更多相关文章

  1. ACM/ICPC 之 最短路-Floyd+SPFA(BFS)+DP(ZOJ1232)

    这是一道非常好的题目,融合了很多知识点. ZOJ1232-Adventrue of Super Mario 这一题折磨我挺长时间的,不过最后做出来非常开心啊,哇咔咔咔 题意就不累述了,注释有写,难点在 ...

  2. 模板C++ 03图论算法 2最短路之全源最短路(Floyd)

    3.2最短路之全源最短路(Floyd) 这个算法用于求所有点对的最短距离.比调用n次SPFA的优点在于代码简单,时间复杂度为O(n^3).[无法计算含有负环的图] 依次扫描每一点(k),并以该点作为中 ...

  3. 最短路 - floyd算法

    floyd算法是多源最短路算法 也就是说,floyd可以一次跑出所以点两两之间的最短路 floyd类似动态规划 如下图: 用橙色表示边权,蓝色表示最短路 求最短路的流程是这样的: 先把点1到其他点的最 ...

  4. HDU1869---(最短路+floyd)

    http://acm.hdu.edu.cn/showproblem.php?pid=1869 思路:最短路+floyd 分析:1 题目是要求所有的数据能否满足“六度分离”,那么我们就想到所有点之间的最 ...

  5. 【bzoj2324】[ZJOI2011]营救皮卡丘 最短路-Floyd+有上下界费用流

    原文地址:http://www.cnblogs.com/GXZlegend/p/6832504.html 题目描述 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘 ...

  6. 【ACM程序设计】求短路 Floyd算法

    最短路 floyd算法 floyd是一个基于贪心思维和动态规划思维的计算所有点到所有点的最短距离的算法. P57-图-8.Floyd算法_哔哩哔哩_bilibili 对于每个顶点v,和任一顶点对(i, ...

  7. poj 3613 经过k条边最短路 floyd+矩阵快速幂

    http://poj.org/problem?id=3613 s->t上经过k条边的最短路 先把1000范围的点离散化到200中,然后使用最短路可以使用floyd,由于求的是经过k条路的最短路, ...

  8. POJ2240 Arbitrage(Floyd判负环)

    跑完Floyd后,d[u][u]就表示从u点出发可以经过所有n个点回到u点的最短路,因此只要根据数组对角线的信息就能判断是否存在负环. #include<cstdio> #include& ...

  9. 最短路--floyd算法模板

    floyd算法是求所有点之间的最短路的,复杂度O(n3)代码简单是最大特色 #include<stdio.h> #include<string.h> ; const int I ...

随机推荐

  1. 【BZOJ1008】【HNOI2008】越狱(数学排列组合题)

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3140  Solved: 1317[Submit][Status] ...

  2. android之网络操作(1)

    一.网络操作 在网络操作中JAVA已经为我提供了一套API用来进行网络操作,在android开发中我们仍可以这套API来做开发.下面通过一个简单的例子来了解一下android下的网络操作. 点击图中下 ...

  3. MongoDB 2.6设置访问权限、设置用户

    MongoDB已经使用很长一段时间了,基于MongoDB的数据存储也一直没有使用到权限访问(MongoDB默认设置为无权限访问限制),今天特地花了一点时间研究了一下,研究成果如下: 注:研究成果基于W ...

  4. hdu2444 判断二分图+最大匹配

    #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...

  5. Day5_作业

     ATM作业:1.额度15000或自定义2.实现购物商城,买东西加入购物车,调用信用卡接口结账3.可以提现,手续费5%4.每月22号出账单,每月10号为还款日,过期未还,按欠款总额万分之5每日计息5. ...

  6. BZOJ-1861 Book 书架 Splay

    1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec Memory Limit: 64 MB Submit: 1010 Solved: 588 [Submit][Stat ...

  7. TortoiseSVN客户端如何更改新的URL

    问题: 我们的服务器换了新的URL地址,这时候我们本地的SVN访问帐号和地址就要重新定义了. 解决步骤: 1:重新定义SVN的URL,右键(TortoiseSVN) → Relocate → 输入你新 ...

  8. php中单例模式的解析说明

    <?php //单例模式 class Dbconn{ private static $_instance=null; protected static $_counter=0; protecte ...

  9. 输入你的性别,身高及体重,判断你的身材是否标准。(用if......else语句)

    Console.WriteLine("请输入你的性别,身高和体重:"); string s = Console.ReadLine(); double h = double.Pars ...

  10. 打印多边形的菱形(for的嵌套)

    Console.WriteLine("请输入一个数字,会出现一个多边的菱形:"); int n = Convert.ToInt32(Console.ReadLine()); ; i ...