Arbitrage

http://acm.hdu.edu.cn/showproblem.php?pid=1217

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
 

解题思路:将货币种类的名字转化为数字,用其作为货币相互转换的下标,然后用Floyd算出货币之间转换后的最多货币,再寻找自己转换为自己大于1的情况,如果有就输出Yes, 没有就输出No

解题代码:

  1. // File Name: Arbitrage 1217.cpp
  2. // Author: sheng
  3. // Created Time: 2013年07月19日 星期五 15时57分20秒
  4. #include <math.h>
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include <string>
  8. #include <iostream>
  9. #include <map>
  10. using namespace std;
  11. const int max_n = ;
  12. map<string, int> k;
  13. double cas[max_n][max_n];
  14. int main()
  15. {
  16. string str1, str2;
  17. int n, m, T = ;
  18. while (scanf("%d", &n) != EOF && n)
  19. {
  20. k.clear();
  21. memset (cas, , sizeof (cas));
  22. for (int i = ; i <= n; i ++)
  23. {
  24. cin >> str1;
  25. k[str1] = i;//转换为数字
  26. }
  27. scanf ("%d", &m);
  28. while (m --)
  29. {
  30. double temp;
  31. cin >> str1;
  32. cin >> temp;
  33. cin >> str2;
  34. cas[k[str1]][k[str2]] = temp;
  35. }
  36. for (int i = ; i <= n; i ++)
  37. for (int j = ; j <= n; j ++)
  38. for (int k = ; k <= n; k ++)
  39. {
  40. if (cas[j][k] < cas[j][i] * cas[i][k])
  41. cas[j][k] = cas[j][i] * cas[i][k];
  42. }
  43. int i;
  44. for (i = ; i <= n; i ++)
  45. if (cas[i][i] > )
  46. {
  47. printf ("Case %d: Yes\n", T++);
  48. break;
  49. }
  50. if (i > n)
  51. printf ("Case %d: No\n", T++);
  52. }
  53. return ;
  54. }

HDU 1217 Arbitrage (Floyd)的更多相关文章

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

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

  2. hdu 1217 (Floyd变形)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)   ...

  3. hdu 1217 Arbitrage (最小生成树)

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

  4. HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...

  5. HDU 1217 Arbitrage(Floyd的应用)

    给出一些国家之间的汇率,看看能否从中发现某些肮脏的......朋友交易. 这是Floyd的应用,dp思想,每次都选取最大值,最后看看自己跟自己的.....交易是否大于一.... #include< ...

  6. hdu 1217 Arbitrage (spfa算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...

  7. hdu 1217 Arbitrage(佛洛依德)

    Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. hdu 1217 Arbitrage

    Flody多源最短路 #include<cstdio> #include<cstring> #include<string> #include<cmath&g ...

  9. hdu 1217 汇率 Floyd

    题意:给几个国家,然后给这些国家之间的汇率.判断能否通过这些汇率差进行套利交易. Floyd的算法可以求出任意两点间的最短路径,最后比较本国与本国的汇率差,如果大于1,则可以.否则不可以. 有向图 一 ...

随机推荐

  1. Android开发遇到的异常及解决办法

    Android开发遇到的错误及解决方法1. Unable to resolve target 'android-7' 解决方案: 修改工程目录下的default.properties文件里的内容tar ...

  2. AOP在 .NET中的七种实现方法

    7Approaches for AOP in .Net AOP在 .NET中的七种实现方法 Here are all the ways that I can think of to add AOPto ...

  3. java环境中基于jvm的两大语言:scala,groovy

    一.java环境中基于jvm的两大语言:scala,groovy 可以在java项目里混编这两种语言: scala:静态语言,多范式语言,糅合了面向对象.面向过程:可以与java和net互操作:融汇了 ...

  4. 深入浅出Spring(五) SpringMVC

    上一篇深入浅出Spring(四) Spring实例分析的博文中,咱们已经可以了解Spring框架的运行原理和实现过程,接下来咱们继续讲解Spring的一个延伸产品——Spring MVC 1.Spri ...

  5. springboot注解

    @RestController和@RequestMapping注解 我们的Example类上使用的第一个注解是 @RestController .这被称为一个构造型(stereotype)注解.它为阅 ...

  6. P1233: [Usaco2009Open]干草堆tower

    这道题,首先想到的就两个,一是贪心,二是动规,然而 1<=N<=100000;1<=w_i<=10000 的数据范围实在不敢恭维,所以说第一想法是错的.仔细一想,首先,我们需要 ...

  7. 带搜索框的下拉框chosen.jQury.js

    下载所需js,css png资源     <link href="chosen.css" rel="stylesheet" type="text ...

  8. Zclip复制页面内容到剪贴板兼容各浏览器

    Zclip:复制页面内容到剪贴板兼容各浏览器 WEB开发中,要让用户复制页面中的一段代码.URL地址等信息,为了避免用户拖动鼠标再进行右键复制操作而可能出现的差错,我们可以直接在页面中放置一个复制按钮 ...

  9. PHP错误The server encountered an internal error or misconfiguration and was unable to complete your re

    我的笔记本电脑上的环境安装了很多次,但是运行项目时总是会报The server encountered an internal error or misconfiguration and was un ...

  10. C#制作高仿360安全卫士窗体(三)

    距上篇C#制作高仿360安全卫士窗体(二)也将近一个多月了,这个月事情还是像往常一样的多.不多我也乐在其中,毕竟我做的是我喜欢做的东西.今天特地抽空把怎么制作文本框写一下.同时也希望有爱好这些玩意的同 ...