Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 19063   Accepted: 8069

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

思路:
抽象出来这题就是要求一个图的最大环,仍然用Floyd算法
要注意下G数组的对角线的值都要是1

#include <iostream>
#include <cstring>
#include <map>
using namespace std; map<string,int> money;
double G[][];
int n,m; void Floyd()
{
for(int k = ;k <= n;k++)
for(int i = ;i <= n;i++)
for(int j = ;j <= n;j++)
if(G[i][j] < G[i][k]*G[k][j])
G[i][j] = G[i][k]*G[k][j];
} int main()
{
int countt = ;
while(cin>>n && n)
{
string tmp;
for(int i = ;i <= n;i++)
{
cin>>tmp;
money.insert(make_pair(tmp,i));
G[i][i] = ;
}
cin>>m;
string t1,t2;
double t;
for(int i = ;i <= m;i++)
{
cin>>t1>>t>>t2;
G[money[t1]][money[t2]] = t;
}
Floyd();
int flag = ;
for(int i = ;i <= n;i++)
if(G[i][i] > ) {
flag = ;
break;
}
if(flag)
cout<<"Case "<<++countt<<": Yes"<<endl;
else
cout<<"Case "<<++countt<<": No"<<endl;
}
return ;
}

POJ-2240的更多相关文章

  1. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

  2. poj 2240 Arbitrage (Floyd)

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

  3. POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环)

    POJ 2240 Arbitrage / ZOJ 1092 Arbitrage / HDU 1217 Arbitrage / SPOJ Arbitrage(图论,环) Description Arbi ...

  4. poj 2240 Arbitrage 题解

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 21300   Accepted: 9079 Descri ...

  5. poj 2240(floyd)

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

  6. POJ 2240 && ZOJ 1082 Arbitrage 最短路,c++ stl pass g++ tle 难度:0

    http://poj.org/problem?id=2240 用log化乘法为加法找正圈 c++ 110ms,g++tle #include <string> #include <m ...

  7. POJ 2240 Arbitrage(floyd)

    http://poj.org/problem?id=2240 题意 : 好吧,又是一个换钱的题:套利是利用货币汇率的差异进行的货币转换,例如用1美元购买0.5英镑,1英镑可以购买10法郎,一法郎可以购 ...

  8. poj 2240 Arbitrage (最短路 bellman_ford)

    题目:http://poj.org/problem?id=2240 题意:给定n个货币名称,给m个货币之间的汇率,求会不会增加 和1860差不多,求有没有正环 刚开始没对,不知道为什么用 double ...

  9. POJ 2240 Arbitrage【Bellman_ford坑】

    链接: http://poj.org/problem?id=2240 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  10. POJ 2240 Arbitrage(判正环)

    http://poj.org/problem?id=2240 题意:货币兑换,判断最否是否能获利. 思路:又是货币兑换题,Belloman-ford和floyd算法都可以的. #include< ...

随机推荐

  1. JuiceSSh破解分析

    JuiceSSH是一款免费的远程ssh客户端,感觉是一款挺优秀的软件,里边有一些高级功能需要购买高级版才能使用,这里便对其对高级功能的破解进行分析. 本文仅用于学习交流使用,请尊重作者,勿在网上肆意发 ...

  2. Java基础知识强化97:final、finally、finally区别

    1. final修饰符(关键字)     如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此,一个类不能既被声明为abstract,又被声明为final.     将 ...

  3. checkbox遍历操作, 提交所有选中项的值

    <div class="content_list pad_10 hidden" > <h3>修改可配送地区</h3> <input typ ...

  4. spring依赖注入源码分析和mongodb自带连接本地mongodb服务逻辑分析

    spring依赖注入本质是一个Map结构,key是beanId,value是bean对应的Object. autowired是怎么将定义的接口与对应的bean类建立联系? <bean name= ...

  5. asp.net微信开发第十篇----使用百度编辑器编辑图文消息,上传图片、微信视频

    经过几天的资料收集,终于完成了该编辑器的图片上传,视频插入功能,视频插入功能主要借用了该编辑器的插入iframe功能,如原始插件图: 修改后的插件图如下(其中我隐藏掉了一些不需要使用的插件功能): 配 ...

  6. tribonacci

    Everybody knows Fibonacci numbers, now we are talking about the Tribonacci numbers: T[0] = T[1] = T[ ...

  7. 添加view类图中的二级菜单

    void CFafdsafasdfasfasView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message han ...

  8. CSS 特效 (教程还是英文的好)

    Border-radius: create rounded corners with CSS! http://www.css3.info/preview/rounded-border/

  9. 转 常用JQuery插件整理

    虽然自己也写过插件,但JQuery插件种类的繁多,大多时候,我还是使用别人写好的插件,这些都是我用了同类插件里较为不错的一些,今天就整理一下公开放出来. UI: jquery.HooRay(哈哈,自己 ...

  10. 细说PHP优化那些事

    我们在用PHP编程的时候,总是想要使自己的程序占用资源最小,运行速度更快,代码量更少.往往我们在追求这些的同时却失去了很多东西.下面我想讲讲我对PHP优化的理解.优化的目的是花最少的代价换来最快的运行 ...