Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17349   Accepted: 7304

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

这一题先要用map将字符转义为数字,每一个字符串用数字代表然后读入图中。

在这一题求的是从起点開始通过一系列的路径最后回到起点是否能找到一条路使得他们的乘积>1,所以map[i][i]初始化就不是1。而是0.这里的方法是用floyd求出每一个通路的最大值,不断的更新以达到求最大值的目的。

開始没有想到。想用深搜来实现,但是这样的方法更简单高效

#include<iostream>
#include<map>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; double g[50][50];
int main()
{
int n,w;
double val;
string s,s1,s2;
map<string,int>q;
int cas=0;
while(~scanf("%d",&n)&&n)
{
memset(g,0,sizeof(g));
for(int i=0;i<n;i++)
{
cin>>s;
q[s]=i;
}
scanf("%d",&w);
while(w--)
{
cin>>s1>>val>>s2;
g[q[s1]][q[s2]]=val;
} for(int k=0;k<n;k++)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
g[i][j]=max(g[i][j],g[i][k]*g[k][j]);
}
}
}
int ok=0;
for(int i=0;i<n;i++)
{
if(g[i][i]>1)
{
ok=1;
break;
}
}
printf("Case %d: %s\n",++cas,ok?"Yes":"No");
}
return 0;
}

poj 2240 floyd算法的更多相关文章

  1. Poj(2240),Floyd求汇率是不是赚钱

    题目链接:http://poj.org/problem?id=2240. Floyd算法修改一下,我要最大路径(通过转汇率变到最大)改成max. #include <iostream> # ...

  2. poj 2240(floyd)

    http://poj.org/problem?id=2240 题意:有些人会利用货币的不用汇率来进行套现,比如1美元换0.5英镑,而1英镑又可以换10法郎,而1法郎又可以换0.21的美元,那么经过货币 ...

  3. 图论---POJ 3660 floyd 算法(模板题)

    是一道floyd变形的题目.题目让确定有几个人的位置是确定的,如果一个点有x个点能到达此点,从该点出发能到达y个点,若x+y=n-1,则该点的位置是确定的.用floyd算发出每两个点之间的距离,最后统 ...

  4. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  5. (poj 3660) Cow Contest (floyd算法+传递闭包)

    题目链接:http://poj.org/problem?id=3660 Description N ( ≤ N ≤ ) cows, conveniently numbered ..N, are par ...

  6. ACM: POJ 3660 Cow Contest - Floyd算法

    链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Descri ...

  7. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  8. POJ 1502 MPI Maelstrom(模板题——Floyd算法)

    题目: BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distri ...

  9. poj 2139 奶牛拍电影问题 floyd算法

    题意:奶牛拍一系列电影,n头牛拍m部电影,同一部电影种的搭档们距离为1,求最小距离? 思路:Floyd 图 最短路径 存图: 初始化图 for (int i = 0; i <= n; i++) ...

随机推荐

  1. 73.node.js开发错误——TypeError: Cannot set property 'XXX' of undefined

    转自:https://blog.csdn.net/fd214333890/article/details/53467429

  2. 5.Maven之(五)Maven仓库

    转自:https://blog.csdn.net/oonmyway1234/article/details/82315777 本地仓库 Maven一个很突出的功能就是jar包管理,一旦工程需要依赖哪些 ...

  3. Linux系统下到哪儿寻找硬件错误

    Linux系统下到哪儿寻找硬件错误       当linux系统出现故障的时候,作为管理员首先要定位错误,现在linux有许多工具都能帮助用户寻找错误,要学会利用他们确定问题.这些工具包括dmesg. ...

  4. ASP.NET MVC Web API 学习笔记---第一个Web API程序---近来很多大型的平台都公开了Web API

    1. Web API简单说明 近来很多大型的平台都公开了Web API.比如百度地图 Web API,做过地图相关的人都熟悉.公开服务这种方式可以使它易于与各种各样的设备和客户端平台集成功能,以及通过 ...

  5. vue 实例的生命周期

      Vue把整个生命周期划分为创建.挂载.更新.销毁等阶段,每个阶段都会给一些"钩子"让我们来做一些我们想实现的动作. 分为以下几个阶段 1.beforeCreate   此阶段为 ...

  6. QQ亿级日活跃业务后台核心技术揭秘

    http://ms.csdn.net/geek/75478 引言 作为本次技术开放日后台架构专场的出品人我今天给大家带来<构造高可靠海量用户服务-SNG数亿级日活跃业务后台核心技术揭秘>, ...

  7. 洛谷——P3384 【模板】树链剖分

    https://www.luogu.org/problem/show?pid=3384#sub 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作 ...

  8. 汉化 Hirens.BootCD 中的 XP 系统

    汉化 Hirens.BootCD 中的 XP 系统 1. 在中文版 XPSP3 镜像中提取所需的文件 原系统为没有作 server pack 的 Windows XP Professional ,在W ...

  9. 团队作业-Beta冲刺(3)

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 这个作业要求在哪里 https://edu.cnblo ...

  10. VNC Server模拟攻击实战

    VNC目前已被广泛应用的一个远程控制程序,很多攻击者对VNC的攻击技术研究热情是高涨的,丝毫不亚于对Windows的远程桌面(3389).PcAnywhere的攻击研究.从最开始爆发出来的VNC的低版 ...