hdu 1217(Floyed)
Arbitrage
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6522 Accepted Submission(s): 3019
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 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.
each test case, print one line telling whether arbitrage is possible or
not in the format "Case case: Yes" respectively "Case case: No".
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
Case 2: No
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)的更多相关文章
- POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)
POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...
- [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)
Arbitrage Problem Description Arbitrage is the use of discrepancies in currency exchange rates to tr ...
- hdu 1217 (Floyd变形)
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1217 Arbitrage (Floyd)
Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...
- hdu 1217 Arbitrage (最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1217 /************************************************* ...
- HDU 1217 Arbitrage(Bellman-Ford判断负环+Floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:问你是否可以通过转换货币从中获利 如下面这组样例: USDollar 0.5 Brit ...
- hdu 1217 Arbitrage (spfa算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意:通过货币的转换,来判断是否获利,如果获利则输出Yes,否则输出No. 这里介绍一个ST ...
- HDOJ 1217 Floyed Template
解题思路:1.map简单应用2.Floyd算法的变形,之后判断dis[i][i],如果大于1,则存在利润! #include <iostream> #include <stdio.h ...
- hdu 1217 汇率 Floyd
题意:给几个国家,然后给这些国家之间的汇率.判断能否通过这些汇率差进行套利交易. Floyd的算法可以求出任意两点间的最短路径,最后比较本国与本国的汇率差,如果大于1,则可以.否则不可以. 有向图 一 ...
随机推荐
- 1911: [Apio2010]特别行动队(斜率优化)
链接 思路 斜率优化dp. 代码 #include<cstdio> #include<algorithm> #include<cstring> #include&l ...
- Android 图片放错位置会拉伸变形
今天做了一个很小的需求,然后需要图片,我给ui要图片.直接给了我三套,还命名 x . xx. 2k 真的一开始都不知道.没有玩过这么正规的.我就用了一张,放到了hdpi下面. 后来同事帮我才知道, 图 ...
- 通俗版解释网关,IP地址,ARP欺骗,DDOS攻击
计算机主机网关的作用是什么? 假设你的名字叫小不点,你住在一个大院子里,你的邻居有很多小伙伴,在门口传达室还有个看大门的李大爷,李大爷就是你的网关.当你想跟院子里的某个小伙伴玩,只要你在院子里大喊一声 ...
- 手把手教你写Windows 64位平台调试器
本文网页排版有些差,已上传了doc,可以下载阅读.本文中的所有代码已打包,下载地址在此. ------------------------------------------------------- ...
- TO_CHAR 和 TO_DATE的一些用法总结
对于初学者来说,日期处理那快一向是问题的集中地.今天刚刚看了个案例,将的就是ORACLE日期处理中的错误,其关键原因,就是TO_CHAR 和TO_DATE的用法不清晰,,事实上,这两个东西也特别容易混 ...
- python学习笔记-基础
1.大小写敏感 2. print (n,f,s1,s2,s3,s4,sep='\n') -- 换行输出 seq='\n' print ('n=%d'%n,'f=%f'%f,'s1=%s'%s1,' ...
- 【java并发编程实战】第二章:对象的共享
1.重要的属性 可见性,不变性,原子性 1.1可见性 当一个线程修改某个对象状态的时候,我们希望其他线程也能看到发生后的变化. 在没有同步的情况下,编译器和处理器会对代码的执行顺序进行重排.以提高效率 ...
- hp raid json
hp机器均已在装OS之前划好raid,统一规格为2*480G SSD, 12*4T SATA ,2*1.6T SSD,其中2*480G SSD做系统盘,划分raid1 已知disk controlle ...
- Nginx下配置codeigniter框架
原来在winserver+Apache环境下工作良好的一个微信公众号后台迁移到阿里云(环境:Ubuntu 64位 | PHP5.4 | Nginx1.6)下却频出 404,403,只能访问CI rou ...
- 软件工程概论课堂测试一————添加新课程(web)
设计思想 三个文件Class_add.java add.jsp addInput.jsp Class_add.java : 内封装方法:连接数据库.向数据库添加课程信息.判断非合理的输入情况.判断 ...