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. EF 基本数据过滤

    没猜错的话, 你们一定会和一大堆查询条件过不去, 重复的写,反复的写, 写到山崩地裂. 今天看了园友的文字:实体框架高级应用之动态过滤 EntityFramework DynamicFilters 我 ...

  2. IDEA SSH

    被IDEA各种虐0.0各种配置,没有人教就百度,别人也弄了点,然后整整花了一天,从eclipse转到IDEA真的不容易啊TT

  3. 翻译-微服务API Gateway

    原文地址:http://microservices.io/patterns/apigateway.html,以下是使用google翻译对原文的翻译. 让我们想象一下你正在建立一个使用微服务模式的网上商 ...

  4. Orchard创建自定义表单

    本文链接:http://www.cnblogs.com/souther/p/4520130.html 主目录 自定义表单模块可以用来获取网站前台用户的信息.自定义表单需要与一个内容类型结合使用.它可以 ...

  5. xml基本操作

    在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.xml是一种可扩展标记语言,可跨平台进行传输,因此xml被广泛的使用在很多地方. 本文由浅入深,首先就xml的 ...

  6. AngularJS——grunt神器的安装

    前言: 刚开始学 angularJS,在慕课网上看的大漠老师的视频(http://www.imooc.com/learn/156),里面刚开始讲述了前端开发-调试-测试所使用的手段和工具,本人对前端开 ...

  7. NABCD模型需求分析

    仓库管理系统的NABCD模型 N-Need仓库管理是与我们日常生活息息相关的问题,随着改革开放的不断深入,经济飞速的发展,企业要想生存.发展,要想在激烈的市场竞争中立于不败之地,没有现代化的管理是万万 ...

  8. Xamarin.Forms——尺寸大小(五 Dealing with sizes)

    如之前所见的大量可视化元素均有自己的尺寸大小: iOS的状态栏高度为20,所以我们需要调整iOS的页面的Padding值,留出这个高度. BoxView设置它的默认宽度和高度为40. Frame的默认 ...

  9. 【BZOJ-1030】文本生成器 AC自动机 + DP

    1030: [JSOI2007]文本生成器 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3253  Solved: 1330[Submit][Stat ...

  10. STL Iterators

    Summary of Chapter 33 STL Iterators from The C++ Programming Language 4th. Ed., Bjarne Stroustrup. - ...