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就可以更新出所有的结果.
import java.util.HashMap;
import java.util.Scanner; public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = ;
while(sc.hasNext()){
int n = sc.nextInt();
if(n==) break;
double [][] map = new double [n][n];
HashMap<String,Integer> hm = new HashMap<String,Integer>();
for(int i=;i<n;i++){
String str = sc.next();
hm.put(str,i);
}
int m = sc.nextInt();
while(m-->){
String str1 = sc.next();
double v = sc.nextDouble();
String str2 = sc.next();
int a = hm.get(str1);
int b = hm.get(str2);
map[a][b]= v;
}
for(int k=;k<n;k++){
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(map[i][j]<map[i][k]*map[k][j]) map[i][j] = map[i][k]*map[k][j];
}
}
}
boolean flag = false;
for(int i=;i<n;i++) if(map[i][i]>){flag=true;break;}
System.out.print("Case "+(t++)+": ");
if(flag) System.out.println("Yes");
else System.out.println("No");
}
}
}

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. PCB工艺要求

    项目 加工能力 工艺详解   层数 1~6层 层数,是指PCB中的电气层数(敷铜层数).目前嘉立创只接受1~6层板.   板材类型 FR-4板材 板材类型:纸板.半玻纤.全玻纤(FR-4).铝基板,目 ...

  2. 3 Mongodb数据查询1

    1.基本查询 方法find():查询 db.集合名称.find({条件文档}) 方法findOne():查询,只返回第一个 db.集合名称.findOne({条件文档}) 方法pretty():将结果 ...

  3. Linux下单机安装部署kafka及代码实现

    技术交流群:233513714 这几天研究了kafka的安装及使用,在网上找了很多教程但是均以失败告终,直到最后想起网络方面的问题最终才安装部署成功,下面就介绍一下kafka的安装部署及代码实现 一. ...

  4. 简易版AI英文问答程序解决

    第四章的作业和实践题要论印象深刻无疑就是AI的那道题了.不得不说一开始看到题目的时候,我真的蒙了很久. 本题要求你实现一个简易版的 AI 英文问答程序,规则是: 1.无论用户说什么,首先把对方说的话在 ...

  5. Java-数据结构之二叉树练习

    本来这个随笔应该在4月17号就应该发出来的.不巧的是,那天晚上收到了offer,然后接下去两天为入职到处跑,20号入职后一直忙,直到最近几天才有时间看看书.然而20多天前就看完的了二叉树,然后17号那 ...

  6. 5.bootstrap栅格 清除浮动

    只要用到栅格,就注意要清除浮动,清除方法就是在父元素的class上加一个clearfix 1.情景: . <div class="col-sm-7"> <div ...

  7. 图解-Excel的csv格式特殊字符处理方式尝试笔记(个人拙笔)

    Excel格式如下.(截图来自,WPS Office) CSV是一种文本格式的Excel文档格式.不支持Excel的字体特效(比如加粗,颜色)等等的保存. 每一行数据用 "\n" ...

  8. 使用java去对比2个带数学公式的字符串

    首先大家看到这个题目,可能会不屑一顾,呵呵,是的,起初我也认为这是个很简单的任务,当任务拿到手里后,经过我作为程序员来讲已经磨炼的无比通透的大脑来讲发现这其实是个坑. 故事的起因是这样的,想开发一款给 ...

  9. django orderby

    https://www.douban.com/group/topic/44329052/

  10. php连接数据库增删改查----多条件查询

    关于查询,可以直接写在主页面上 来进行查询 首先,先建立一个表单 <form method="post" action="crud.php"> &l ...