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,则可以.否则不可以. 有向图 一 ...
随机推荐
- 2286: [Sdoi2011]消耗战
2286: [Sdoi2011]消耗战 链接 分析 虚树练习题. 构建虚树,在虚树上DP. 跟着gxb学虚-tree... 代码 #include <cstdio> #include &l ...
- Installation error: INSTALL_FAILED_CANCELLED_BY_USER
我的手机本来是支持Androidstadio 调试手机的,我手机小米的,后来,系统升级了,我也没在意,第二天上班,已运行就报错: Installation error: INSTALL_FAILED_ ...
- Python数据类型三
一.帮助 如果想知道一个对象(object)更多的信息,那么可以调用help(object)!另外还有一些有用的方法,dir(object)会显示该对象的大部分相关属性名,还有object._ doc ...
- mybatis异常:There is no getter for property named 'xxx' in 'xxx'
在使用mybatis查询的时候出现了下面的异常: org.apache.ibatis.reflection.ReflectionException: There is no getter for pr ...
- 【非原创】tomcat 安装时出现 Failed to install Tomcat7 service
tomcat 安装时出现 Failed to install Tomcat7 service 今天在安装tomcat时提示 Failed to install Tomcat7 service了,花了大 ...
- Python写的计算器程序(主要目的在于熟悉下正则表达式)
import res = '1-2*((60-30-(-40/5)*(9-2*5/3-7/3*99/4*2998-10*568/14.3))+(-4*3)/16-3)'s2 = 1-2*((60-30 ...
- 带外键Mysql
带外键的表格的查询 复制代码 //////////////////查询指定表外键约束 select a.name as 约束名, object_name(b.parent_object_id) as ...
- VC调试篇:减少运行时错误,中断所有异常
问题简述 我在Win7下写的MFC程序,想让它在winXP下运行.一般情况下,如果所有的依赖库都可以在XP下运行的话,那么在XP下运行时没问题的.但是,结果却... 本来程序在win7下运行得好好的, ...
- 【CDN】- 什么是CDN
高冷科普: CDN,Content Delivery Network缩写,即内容分发网络.通过在网络各处放置节点服务器所构成的在现有的互联网基础之上的一层智能虚拟网络,CDN系统能够实时地根据网络流量 ...
- CF10D LCIS (动态规划)
题目链接 Solution 动态规划. 令 \(f_{i,j}\) 表示 \(a\) 数组前 \(i\) 个和 \(b\) 数组前 \(j\) 所得的最长的 LCIS . 转移很好想: \(a_i!= ...