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

最短路,一直很纠结,一直搞不懂,第一次自己A最短路,看着模板敲啊

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
struct node
{
int a,b;
double v;
} g[];//储存钱之间的汇率
int main()
{
int m,n,i,j,num=;
double v,dis[];
char money[],moneyt[];//谁让钱是字符串呢
while(cin>>n&&n)
{
num++,n++;
map<string,int>mapp;//为了方便把钱编号,用数组可以模拟,太麻烦
map<string,int>::iterator iter;//声明迭代器
for(i=; i<n; i++)
{
cin>>money;
mapp.insert(pair<string,int>(money,i));//插入钱和序号,相当于编号
}
cin>>m;
for(i=; i<m; i++)
{
scanf("%s %lf %s",money,&v,moneyt);//输入兑换比例
iter=mapp.find(money);//查找对应序号
g[i].a=iter->second;
g[i].v=v;
iter=mapp.find(moneyt);//查找对应序号
g[i].b=iter->second;
}
memset(dis,,sizeof(dis));//标记数组置零
dis[]=;
for(i=; i<n; i++)//n-1次松弛
for(j=; j<m; j++)
if(dis[g[j].b]<dis[g[j].a]*g[j].v)
dis[g[j].b]=dis[g[j].a]*g[j].v;
int flag=;
for(j=; j<m; j++)//还可以继续变大,就说明可以赚钱啊
if(dis[g[j].b]<dis[g[j].a]*g[j].v)
flag=;
printf("Case %d: ",num);
if(flag)
printf("Yes\n");
else
printf("No\n");
}
return ;
}

Arbitrage的更多相关文章

  1. poj 2240 Arbitrage

    Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java class name ...

  2. UVa 104 - Arbitrage(Floyd动态规划)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  3. Arbitrage(bellman_ford)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16652   Accepted: 7004 Descri ...

  4. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

  5. poj-------(2240)Arbitrage(最短路)

    Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15640   Accepted: 6563 Descri ...

  6. ZOJ 1092 Arbitrage

    原题链接 题目大意:Arbitrage这个单词的解释是“套利交易”,就是利用几个币种之间的汇率差价来赚钱.比如人民币兑美元6:1,美元兑欧元1.5:1,欧元兑人民币10:1,那么用9元人民币可以换1. ...

  7. poj 2240 Arbitrage bellman-ford算法

    点击打开链接 Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13434   Accepted: 5657 ...

  8. HDU 1217 Arbitrage (Floyd)

    Arbitrage http://acm.hdu.edu.cn/showproblem.php?pid=1217 Problem Description Arbitrage is the use of ...

  9. POJ 2240 Arbitrage (求负环)

    Arbitrage 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/I Description Arbitrage is the ...

  10. POJ2240——Arbitrage(Floyd算法变形)

    Arbitrage DescriptionArbitrage is the use of discrepancies in currency exchange rates to transform o ...

随机推荐

  1. 解决IE6,IE7不能隐藏绝对定位溢出的内容

    令人蛋疼的IE,IE6/IE7下父元素有相对/绝对定位时,子元素在IE6和IE7下overflow:hidden;失效. 情况一:(在parent上增加position:relative) <s ...

  2. ExtractFileDir 与 ExtractFilePath 的差别

    ExtractFileDir 与 ExtractFilePath 的差别 ExtractFileDir 从文件名称中获取文件夹名(文件不在根文件夹下时取得的值后没有"/",在根文件 ...

  3. kswapd0、kjournald、pdflush、kblocked、migration进程含义 转

    kswapd0.kjournald.pdflush.kblocked.migration进程含义 1.kswapd0 Linux uses kswapd for virtual memory mana ...

  4. linux系统下安装wget。

    我们先安装linux系统比如centos7.1里面有的就没有wget下载工具.wget这个命令就不可以使用. 我们使用 yum -y install wget yum install perl 会出现 ...

  5. C++链表与键值对

    <算法>一书中,在算法3.1中提到了Map的实现,这里根据书上的思想,用单向链表简单写了写. #ifndef SEQUENTIAL_H #define SEQUENTIAL_H templ ...

  6. 开源的Android开发框架-------PowerFramework使用心得(四)数据库管理DBFarmer

    DBFarmer是PowerFramework数据库管理工具的集合. 可以进行对象的存储,添加了setter和getter的参数会被收录到数据库中,每个参数作为一个项,int类型的id或_id会被作为 ...

  7. 某PHP代码加密

    <?php /* 本程序已加密: 2014-11-15 10:10:11 */ xs_run('JGxosS9QplmqLA6qjYo/LiX5ecUe0DH7p42Ww/Mdkf5/ybZDs ...

  8. Orace数据库锁表的处理与总结<摘抄与总结二>

    当Oracle数据库发生TX锁等待时,如果不及时处理常常会引起Oracle数据库挂起,或导致死锁的发生,产生ORA-60的错误. TX锁等待的分析 Oracle数据库中一般使用行级锁. 当Oracle ...

  9. kvc简单实现

      除了一般的赋值和取值的方法,我们还可以用Key-Value-Coding(KVC)键值编码来访问你要存取的类的属性 kvc: kvc    key value coding 键值对编码 可以通过 ...

  10. 使用OC开发phonegp 组件

    使用OC开发phonegp 组件 1. 使用oc 对phonegp中的组件近些开发,首先具体的pgonegp跟nativecode之间的一些优劣就不说了,开发phonegp 对应的组件主要就是使用na ...