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

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

题解:

这题是寻找是否存在一种钱币使得交换一圈后可以盈利。就是以任意为起点,找一个终点使得d[i][j]*d[j][i]>1.0即可;

就是Folyd处理,然后判断即可,水题;

AC代码为:

//水题,乘法最短路,格式真的。。。 

#include<bits/stdc++.h>

using namespace std;

double val,dis[50][50];

int n,m,temp,Cas=1;

map<string,int> mp;

void Floyd()

{

    for(int k=1;k<=n;k++)

    {

        for(int i=1;i<=n;i++)

        {

            for(int j=1;j<=n;j++)

                dis[i][j]=max(dis[i][j],dis[i][k]*dis[k][j]);

        }

    }

}

int main()

{

    ios::sync_with_stdio(false);

    while(cin>>n , n)

    {   

        string s1,s2;

        temp=1; mp.clear();

        memset(dis,0,sizeof dis);

        for(int i=1;i<=n;i++) 

        {

            cin>>s1;

            mp[s1]=temp++;

        }

        cin>>m;

        for(int i=1;i<=m;i++)

        {

            cin>>s1>>val>>s2;

            dis[mp[s1]][mp[s2]]=val;    

        }

        if(n==1 && dis[1][1]>1.0) 

        {

            cout<<"Case "<<Cas++<<": "<<"Yes"<<endl;

            //cout<<endl;

            continue;   

        }

        Floyd();

        bool flag=false;

        for(int i=1;i<=n;i++)

        {

            for(int j=1;j<=n;j++)

                if(i==j) continue;

                else if(dis[i][j]*dis[j][i]>1.0)

                {

                    flag=true;

                    break;

                }

            if(flag) break;

        }

        if(flag)  cout<<"Case "<<Cas++<<": "<<"Yes"<<endl;

        else  cout<<"Case "<<Cas++<<": "<<"No"<<endl;

        //cout<<endl;

    }

    

    return 0;

}

HDU1217-Arbitrage(乘法最短路)的更多相关文章

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

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

  2. HDOJ 1217 Arbitrage (最短路)

    题意:每两种货币之间都有不同的汇率  如果换回自己最后是赚的 输出Yes 否则是No 因为最多只有三十种货币 所以用Floyd是可行的 与一般的最短路板子不同的地方 汇率是要乘而不是加 如果乘上一个小 ...

  3. HDOJ 1217 Arbitrage(拟最短路,floyd算法)

    Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. hdu1217 Arbitrage

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

  5. HDU1217:Arbitrage(SPFA)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1217 题目大意 在每种钱币间进行各种交换,最后换回自己如果能赚,那么就Yes,否则No 注意应为有负权 ...

  6. 洛谷P3237 米特运输 [HNOI2014] hash/二进制分解

    正解:hash/二进制分解 解题报告: 传送门! umm首先提取下题意趴QAQ 大概是说给一棵树,每个点有一个权值,要求修改一些点的权值,使得同一个父亲的儿子权值相同,且父亲的权值必须是所有儿子权值之 ...

  7. DFS判断正环

    hdu1217 Arbitrage Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

    题目传送门 /* 最短路:Floyd模板题 只要把+改为*就ok了,热闹后判断d[i][i]是否大于1 文件输入的ONLINE_JUDGE少写了个_,WA了N遍:) */ #include <c ...

  9. POJ 2240 Arbitrage(最短路 套汇)

    题意  给你n种币种之间的汇率关系  推断是否能形成套汇现象  即某币种多次换为其他币种再换回来结果比原来多 基础的最短路  仅仅是加号换为了乘号 #include<cstdio> #in ...

随机推荐

  1. Dart编程语言从基础到进阶1

    Dart编程语言从基础到进阶Dart的语言的发展史以及Dart能做什么未来发展怎么样等等问题我们在这里是不讨论的.我相信既然选择了来学习它,那你内心基本已经认可了它,所以我们废话不多说直接进入主题. ...

  2. SpringMVC错误:nested exception is java.lang.IncompatibleClassChangeError: class org.springframework.core.type.clas

    这是jar包冲突引起的 spring-core.jar已经有asm 所以不用再单独导入asm包了

  3. Vue项目性能优化整理

    以下方式基于 @vue/cli 快速搭建的交互式项目脚手架 1. 路由懒加载 当打包构建应用时,JavaScript 包会变得非常大,影响页面加载.如果我们能把不同路由对应的组件分割成不同的代码块,然 ...

  4. ArcGIS API For Javascript:新增热力图层的方法

    当我们要制作一个热力图层,可以通过以下 3 步来实现: 引入类 在 require 中需引入 "esri/layers/FeatureLayer", "esri/rend ...

  5. fiddler工具使用大全

    Fiddler基础知识 Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的,使用的代理地址是:127.0.0.1,端口默认为8888,我们也可以通过设置进行修改. 代理就是在 ...

  6. .Net Core 使用NPOI导入数据

    一.搭建环境 1.新建ASP.NET Core Web 应用程序 2.选择API 3.引用Swashbuckle.AspNetCore NuGet 包进行安装. Swashbuckle.AspNetC ...

  7. C语言入门教程: 一个简单的实例

    对于学习要保持敬畏! 语言不只是一种工具,还是一种资源,因此,善待它,掌握它!   我们知道,对于未知通常都会充满好奇和畏惧,既想了解它,又害怕神秘面纱隐藏的不确定性.对于一门编程语言同样如此,我将以 ...

  8. 扛把子组20191031-2 Beta阶段贡献分配规则

    此作业的要求参见https://edu.cnblogs.com/campus/nenu/2019fall/homework/9910 队名:扛把子 组长:孙晓宇 组员:宋晓丽 梁梦瑶 韩昊 刘信鹏 B ...

  9. Java流程控制之(一)条件

    目录 条件语句 单if情况 单if/else情况 if/else多分支情况 switch条件语句 条件语句+循环语句,直接甩图甩代码! 条件语句 Java希望在某个条件为真时执行相应的语句. 单if情 ...

  10. day 13 生成器函数 表达式 推导式

    今日主要内容 1. 生成器和生成器函数 生成器的本质就是迭代器 生成器的三种创建办法: 1.通过生成器函数 2.通过生成器表达式创建生成器 3.通过数据转换 2. 生成器函数: 函数中包含了yield ...