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. JavaScript--数据结构与算法之链表实现约瑟夫环

    传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第三个人时将 ...

  2. C# WPF开源控件库MaterialDesign介绍

    介绍 1.由于前端时间萌发开发一个基础架构得WPF框架得想法, 然后考虑到一些界面层元素统一, 然后就无意间在GitHub上发现一个开源WPF UI, 于是下载下来了感觉不错. 官网地址:http:/ ...

  3. CSDN-markdown语法之怎样插入图片

    文件夹 图片上传方式 插入在线图片 插入本地图片 图片链接方式 行内式图片链接 參考式图片链接 几个问题探讨 问题1:图片上传和图片链接两种方式的差别 问题2:Markdown中怎样指定图片的高和宽? ...

  4. [Express] Upload Files with Express

    In this lesson we create a new Express web server app for handling file uploads and persisting them ...

  5. HDU 3487(Play with Chain-Splay)[template:Splay]

    Play with Chain Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. notification-应用实例

    这几天接触到了notification,现在就把它的常用方法总结下. 直接看如下代码就行了 ComponentName componetName = new ComponentName("c ...

  7. 18.C语言多线程总结

    创建一个线程 _beginthread(myfun, , NULL);//返回值是一个HANDLE hd[i] = CreateThread(NULL, , add, NULL, , NULL);// ...

  8. Manning.EJB.3.in.Action.2nd.Edition

    Manning.EJB.3.in.Action.2nd.Edition http://files.cnblogs.com/files/rojas/EJB_3_in_Action_2nd_Edition ...

  9. Ajax用法

    1.什么是 Ajax? Ajax,英文名 Asynchronous JavaScript and XML,也就是异步的 JavaScript 和 XML.它不是一门新的语言,而是一种使用现有标准的新方 ...

  10. JavaScript的字符串、数组以及DOM操作总结

    (一)JavaScript字符串操作 JavaScript的字符串就是用' '或" "括起来的字符表示,日常的学习中有时候需要对字符串进行相关的操作.例如要获取字符串某个指定位置的 ...