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. Jenkins的plugin开发

    Jenkins强大的功能主要靠其丰富的plugin体现,之前的一篇博客<Jenkins安装plugin>中介绍了如何找到并安装需要的plugin.虽然目前已经有大量非常优秀的plugin可 ...

  2. SunDay天气——开放源代码

    前段时间也些小忙,一直没有时间去弄Github,所以源代码一直没有放出来. 本周末特抽了些时间出来,熟悉了下Github,并把源代码给弄了去.欢迎大牛重吐槽.指导...... 费话不多说,上图. 近期 ...

  3. hdu 3729 I'm Telling the Truth 二分图匹配

    裸的二分图匹配.需要输出方案. #include<cstdio> #include<cstring> #include<vector> #include<al ...

  4. UML进行Linux内核调试

    http://www.lenky.info/ http://blog.csdn.net/ztz0223/article/details/7874759 http://user-mode-linux.s ...

  5. linux进程调度之 FIFO 和 RR 调度策略---SYSTEMTAP

    http://blog.chinaunix.net/uid-24774106-id-3379478.html http://blog.chinaunix.net/uid-24774106-id-337 ...

  6. 旅行喵 React Native 技术实践

    旅行喵,是一款帮助用户快乐旅行的APP. 第一版的首打功能是行程定制,和景点信息介绍.大家可以在上面做非常简单的偏好选择,通过我们的智能算法生成适合自己的旅行路线. 为什么要用RN呢? 首先,相对于其 ...

  7. 转 sqlserver字段描述相关操作sql

    可以自己查询系统表: SELECT o.name AS tableName, c.name AS columnName, p.[value] AS Description FROM sysproper ...

  8. 强制关闭myeclipse出现的问题

    重启时,可能会出现打不开关闭前所在的workspace.其他workspace可以正常打开. 今天遇到这个问题,以前就遇到过,但是忘记如何解决了.今天在我等了十多分钟后,神奇的myeclipse自己起 ...

  9. qrcode-php生成二维码

    调用PHP QR Code非常简单,如下代码即可生成一张内容为"http://www.baidu.com"的二维码. include 'phpqrcode.php'; QRcode ...

  10. idea 配置node Run

    1.node 2.nodemon 支持热部署 3.supervisor  支持执部署