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. OC语言自学基础知识总结

    一.成员变量的作用域 二.点语法 三.构造方法 四.分类 五.类的本质 六.自动生成getter和setter方法 七.description方法 八.id类型 九.SEL 一.成员变量的作用域 @p ...

  2. Kali Rolling 系统配置总结 (Updateing)

    主系统Kali Linux确实好用,继<Kali~2018安装后的配置>之后,自己又全面详细的总结了关于Kali Linux系统安装后的配置,<Kali Rolling 系统配置总结 ...

  3. python-语言元素

    变量命令 对于每个变量我们需要给它取一个名字.在python中,变量命名需要遵循一下这些必须遵守硬性规则和强烈建议遵守的非硬性规则. 硬性规则 变量名由字母(广义的Unicode字符,不包括特殊字符) ...

  4. Java——内部类详解

    说起内部类,大家肯定感觉熟悉又陌生,因为一定在很多框架源码中有看到别人使用过,但又感觉自己使用的比较少,今天我就带你具体来看看内部类. 内部类基础 所谓内部类就是在类的内部继续定义其他内部结构类. 在 ...

  5. 2019-11-3:渗透测试,基础学习,bypass类型笔记

    等价字符 空格:%20,+,(),%0a,%09,%a0,%0b,%0c,%0d,/**/等 =:like,regexp,liker,<>,! =等 and:&& or:x ...

  6. Reactor和Proactor模型

    背景 前面介绍了I/O多路复用模型,那有了I/O复用,有了epoll已经可以使服务器并发几十万连接的同时,还能维持比较高的TPS,难道还不够吗?比如现在在使用epoll的时候一般都是起个任务,不断的去 ...

  7. Unity 工作经历+近期面试经历(二)

    注册博客园后,我原本打算每一份工作经历都记录下来.但是,这份工作已经换了半年了,好几次想要写,又不知道该怎么写.太多的负能量.我始终相信,情绪是会传染的.我基本决定放弃写这篇文章了.就让时间去淡化经历 ...

  8. 前端vue如何下载或者导出word文件和excel文件

    前端用vue怎么接收并导出文件 window.location.href = "excel地址" 如果是 get 请求,那直接换成 window.open(url) 就行了 创建一 ...

  9. Win10无法安装.NET Framework3.5的解决办法

    诸位网友如果工作中使用WIN10遇到如图的这种问题,现将解决办法整理如下: 一.第一步就是修复系统:按“Windows+X”点击“Windows PowerShell(管理员)&命令提示符(管 ...

  10. centos7安装fail2ban

    fail2ban是一款非常实用的安全软件,通过监视系统日志,设置错误登陆次数,可阻挡暴力密码攻击. 1.安装epelyum install epel-release -y 2.安装fail2banyu ...