Arbitrage

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6522    Accepted Submission(s): 3019

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
 
题意:n种货币,有m种汇率,问某种货币能否通过汇率交易之后变得更多?
题解:假设某种货币最初是1,然后寻找某条回路,看最后它能不能变得比1更大,用Floyed就可以更新出所有的结果.
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7. int t = ;
  8. while(sc.hasNext()){
  9. int n = sc.nextInt();
  10. if(n==) break;
  11. double [][] map = new double [n][n];
  12. HashMap<String,Integer> hm = new HashMap<String,Integer>();
  13. for(int i=;i<n;i++){
  14. String str = sc.next();
  15. hm.put(str,i);
  16. }
  17. int m = sc.nextInt();
  18. while(m-->){
  19. String str1 = sc.next();
  20. double v = sc.nextDouble();
  21. String str2 = sc.next();
  22. int a = hm.get(str1);
  23. int b = hm.get(str2);
  24. map[a][b]= v;
  25. }
  26. for(int k=;k<n;k++){
  27. for(int i=;i<n;i++){
  28. for(int j=;j<n;j++){
  29. if(map[i][j]<map[i][k]*map[k][j]) map[i][j] = map[i][k]*map[k][j];
  30. }
  31. }
  32. }
  33. boolean flag = false;
  34. for(int i=;i<n;i++) if(map[i][i]>){flag=true;break;}
  35. System.out.print("Case "+(t++)+": ");
  36. if(flag) System.out.println("Yes");
  37. else System.out.println("No");
  38. }
  39. }
  40. }

hdu 1217(Floyed)的更多相关文章

  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. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

    Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...

  3. hdu 1217 (Floyd变形)

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

  4. HDU 1217 Arbitrage (Floyd)

    Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...

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

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

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

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

  7. hdu 1217 Arbitrage (spfa算法)

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

  8. HDOJ 1217 Floyed Template

    解题思路:1.map简单应用2.Floyd算法的变形,之后判断dis[i][i],如果大于1,则存在利润! #include <iostream> #include <stdio.h ...

  9. hdu 1217 汇率 Floyd

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

随机推荐

  1. P3818 小A和uim之大逃离 II(洛谷月赛)

    P3818 小A和uim之大逃离 II 题目背景 话说上回……还是参见 https://www.luogu.org/problem/show?pid=1373 吧 小a和uim再次来到雨林中探险.突然 ...

  2. CodeIgniter学习笔记四:CI中的URL相关函数,路由,伪静态,去掉index.php

    一.URL相关函数 1.加载url模块 加载url有两种方式: a.自动加载:在 application/config/autoload.php 中开启 $autoload['helper'] = a ...

  3. JMeter学习笔记(十) 计数器

    前面写了导出文件接口的测试,对于导出文件的文件名称,为了不重复(即不覆盖之前的文件),可以添加一个计数器来设置不同的index,另外也可以借助函数助手. 下面是我使用到的关于计数器的简单应用,其他的自 ...

  4. pytest单元测试框架

    一.安装方式 1.安装命令:pip install pytest 2.html安装插件:pip install pytest -html 二.pytest执行指定测试用例 1.思想:通过对测试用例进行 ...

  5. 第二阶段团队冲刺-two

    昨天: 处理第一次启动服务器500的问题. 今天: 修复博客作业查询功能. 遇到的问题: 无.

  6. .NET Framework中的过时类型

    文章:.NET Framework 中的过时类型 url地址:https://docs.microsoft.com/zh-cn/dotnet/framework/whats-new/obsolete- ...

  7. CSLA多语言设置

    1.在程序运行文件夹例如“\Bin\Debug\”中包含csla生成的资源文件: 2.在程序运行时,设置CSLA的当前语言为你想要的语言,例如:Csla.Properties.Resources.Cu ...

  8. hadoop2.5.2学习及实践笔记(四)—— namenode启动过程源码概览

    对namenode启动时的相关操作及相关类有一个大体了解,后续深入研究时,再对本文进行补充 >实现类 HDFS启动脚本为$HADOOP_HOME/sbin/start-dfs.sh,查看star ...

  9. MVC4.0 bug 神奇的是事情 bool 值变成了 onclick ,非常奇怪的

    foreach (var item in ViewBag.PhotoGroupList) { // 这里很奇怪 item.IS_DISPLAY  是布尔值 如果直接写 @item.IS_DISPLAY ...

  10. npm & npm config

    npm command show npm config https://docs.npmjs.com/cli/config https://docs.npmjs.com/cli/ls https:// ...