Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 16652   Accepted: 7004

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 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

Source

 #include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
const int M = , inf = 0x3f3f3f3f;
struct Arbitrage
{
int u , v ;
double r ;
}e[M * M];
int n , m ;
char cur[M][] ;
char a[] , b[] ;
double d[M] ; void init (char a[] , char b[] , int no)
{
for (int i = ; i <= n ; i++) {
if (strcmp (cur[i] , a) == )
e[no].u = i ;
if (strcmp (cur[i] , b) == )
e[no].v = i ;
}
} int bellman_ford (int o)
{
for (int i = ; i <= n ; i++)
d[i] = ;
d[o] = 1.0 ;
double temp = 1.0 ;
bool flag ;
for (int i = ; i <= n ; i++) {
flag = ;
for (int j = ; j < m ; j++) {
if (d[e[j].v] < d[e[j].u] * e[j].r) {
d[e[j].v] = d[e[j].u] * e[j].r ;
flag = ;
}
if (d[o] > temp) {
return true ;
}
}
}
return false ;
} int main ()
{
//freopen ("a.txt" , "r" , stdin) ;
int ans = ;
while (~ scanf ("%d" , &n)) {
if (n == )
break ;
getchar () ;
for (int i = ; i <= n ; i++) {
gets (cur[i]) ;
}
scanf ("%d" , &m) ;
for (int i = ; i < m ; i++) {
cin >> a >> e[i].r >> b ;
init (a , b , i) ;
}
/* for (int i = 0 ; i < m ; i++) {
printf ("u = %d , v = %d , r = %.2f\n" , e[i].u , e[i].v , e[i].r) ;
}*/
int i ;
for (i = ; i <= n ; i++) {
if (bellman_ford (i)) {
printf ("Case %d: Yes\n" , ans++) ;
// printf ("Arbitrage num : %d\n" , i) ;
break ;
}
/*printf ("%d team: \n" , i) ;
for (int j = 1 ; j <= n ; j++)
printf ("%.2f " ,d[j]) ;
puts ("") ; */
}
if (i == n + )
printf ("Case %d: No\n" , ans++) ;
}
return ;
}

Arbitrage(bellman_ford)的更多相关文章

  1. [ACM] hdu 1217 Arbitrage (bellman_ford最短路,推断是否有正权回路或Floyed)

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

  2. POJ 2240 Arbitrage Bellman_ford 判读是否存在正环

    和POJ1860差不多,就是用bellmanford判读是否存在正环,注意的是同种货币之间也可以交换,就是说:A货币换A货币汇率是2的情况也是存在的. #include<stdio.h> ...

  3. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  4. POJ 2240 - Arbitrage(bellman_ford & floyd)

    题意: 给出一些货币和货币之间的兑换比率,问是否可以使某种货币经过一些列兑换之后,货币值增加. 举例说就是1美元经过一些兑换之后,超过1美元.可以输出Yes,否则输出No. 分析: 首先我们要把货币之 ...

  5. poj 2240 Arbitrage (最短路 bellman_ford)

    题目:http://poj.org/problem?id=2240 题意:给定n个货币名称,给m个货币之间的汇率,求会不会增加 和1860差不多,求有没有正环 刚开始没对,不知道为什么用 double ...

  6. poj 2240 Arbitrage(Bellman_ford变形)

    题目链接:http://poj.org/problem?id=2240 题目就是要通过还钱涨自己的本钱最后还能换回到自己原来的钱种. 就是判一下有没有负环那么就直接用bellman_ford来判断有没 ...

  7. POJ 1860 Currency Exchange + 2240 Arbitrage + 3259 Wormholes 解题报告

    三道题都是考察最短路算法的判环.其中1860和2240判断正环,3259判断负环. 难度都不大,可以使用Bellman-ford算法,或者SPFA算法.也有用弗洛伊德算法的,笔者还不会SF-_-…… ...

  8. Nyoj Arbitrage(Floyd or spfa or Bellman-Ford)

    描述Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a curren ...

  9. POJ 2240 - Arbitrage - [bellman-ford求最短路]

    Time Limit: 1000MS Memory Limit: 65536K Description Arbitrage is the use of discrepancies in currenc ...

随机推荐

  1. 20145215《Java程序设计》第9周学习总结

    20145215<Java程序设计>第九周学习总结 教材学习内容总结 整合数据库 JDBC入门 JDBC是用于执行SQL的解决方案,开发人员使用JDBC的标准接口,数据库厂商则对接口进行操 ...

  2. 浅谈JS面向对象之创建对象

    hello,everybody,今天要探讨的问题是JS面向对象,其实面向对象呢呢,一般是在大型项目上会采用,不过了解它对我们理解JS语言有很大的意义. 首先什么是面向对象编程(oop),就是用对象的思 ...

  3. BASE64Decoder 编码(sun.jar)

    Base64 是网络上最常见的用于传输8Bit 字节代码的编码方式之一,大家可以查看RFC2045 -RFC2049 ,上面有MIME 的详细规范.  Base64 要求把每三个8Bit 的字节转换为

  4. libtool: Version mismatch error 解决

    在编译一个软件的时候,在 ./configure 和 make  之后可能会出现如下错误: libtool: Version mismatch error.  This is libtool 2.4. ...

  5. 有四中方法可以实现PHP的伪静态,你造吗?

    说起伪静态的实现方案,你是不是很爽快的回答"简单,配置下apache的重写规则就行了嘛" 但是你有没有发现这种情况,你最近弄了很多新功能,每天上几个新功能,每天都有好多伪静态配置, ...

  6. spring+mybatis实现读写分离

    springmore-core spring+ibatis实现读写分离 特点 无缝结合spring+ibatis,对于程序员来说,是透明的 除了修改配置信息之外,程序的代码不需要修改任何东西 支持sp ...

  7. ThinkPHP之MVC与URL访问

    一.初探 我们在apache的www目录下创建一个文件夹,其命名为我们的应用名.然后通过入口文件生成我们的应用. 当我们用ThinkPHP创建好一个应用后,其目录结果如下所示 那么我们如何来访问我们应 ...

  8. javascript基础知识拾遗

    1 下面列出的值被当作假 false null undefined '' 0 NaN 其它所有值被当作是真 console.log(undefined || true); //true console ...

  9. The web application [/codeMarket] registered the JBDC driver[.........] but failed to unregister it when the web application was stopped. To prevent

    如果你报错了上面的这个严重,那么你的tomcat版本一定是在6.0.25之上的 原因:tomcat 6.025以后在sever.xml中引入了内存泄露侦测,对于垃圾回收不能处理的对像,它就会做日志. ...

  10. onethink常用标签的使用示例

    首页文章模型列表输出: <article:list name="article" category="2" row="3" order ...