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. MVC5 + EF6 + Bootstrap3 (12) 新建数据

    Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc5-ef6-bs3-get-started-create.html 系列教程:MVC5 + EF6 + ...

  2. Linq动态查询简易解决之道(原创)

    因为项目需要使用Linq来查询数据,但是在多条件查询时,需要使用一大堆if(...!=string.empty)等判断条件感觉不是很优雅.网上搜索以下,大概找到了两种办法,一种是老外写的一个类,感觉用 ...

  3. [整理] ES5 词法约定文档树状图

    将ES5 词法说明整理为了树状图,方便查阅,请自行点开小图看大图:

  4. Linq之Linq to Objects

    目录 写在前面 系列文章 linq to objects 总结 写在前面 上篇文章介绍了linq的延迟加载特性的相关内容,从这篇文章开始将陆续介绍linq to Objects,linq to xml ...

  5. EntityFramework_MVC4中EF5 新手入门教程之四 ---4.在EF中创建更复杂的数据模型

    在以前的教程你曾与一个简单的数据模型,由三个实体组成.在本教程中,您将添加更多的实体和关系,并通过指定格式. 验证和数据库映射规则,您将自定义数据模型.你会看到自定义的数据模型的两种方式: 通过添加属 ...

  6. 第三章:Javascript类型、值和变量。

    计算机程序的运行需要对值(value)比如数字3.14或者文本"hello world"进行操作,在编程语言中,能够表示并操作的值的类型叫做数据类型(type),编程语言最基本的特 ...

  7. 第四次个人作业--必应词典(PC端)分析

    part.1 调研.评测 ·bug简述: 1.初始使用时,我觉得划译功能很方便,可是在QQ和浏览器上多次使用划译功能后,我发现并不是每次划译总会做出翻译,而是显示原文而已.如下图: 2.敲代码是出现b ...

  8. ansible 的组件inventory

    P44 Ansible 的默认的inventory的是一个静态的ini格式的文件/etc/ansible/hosts. 我们还可以通过ansible_hosts环境变脸指定或者运行ansible和an ...

  9. 使用gitlab+jenkins+saltstack+rsync自动部署Web应用

    转载:http://www.ithao123.cn/content-8128849.html

  10. iOS不得姐项目--appearance的妙用,再一次设置导航栏返回按钮,导航栏左右按钮的封装(巧用分类)

    一.UI_APPEARANCE_SELECTOR 彩票项目中appearance的用法一直没有搞明白,这次通过第二个项目中老师的讲解,更深一层次的了解到了很多关于appearance的作用以及使用方法 ...