Arbitrage
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 给你一个汇率表,找出其中有没有一种能创造无限金钱的环?这是我第一次写的比较清醒的spfa。
spfa判环有两种:
1.dfs:判断某个点是否在同一条路径出现多次
2.bfs:判断某个点入队的次数是否大于自身的入度
代码如下:
 #include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
#define M 9000
map <string,int> money;//将货币映射成数字
struct Edge
{
int u,v;
double rate;
}edge[M];
vector <int> g[];//存图
bool inque[];//标记是否入队
double price[];//到达某点的最大汇率(类似距离)
int cnt[];//标记点的入队次数
int in_degree[];//入度
int toNum (string x)
{
return money[x];
}
int n,m;
void init()
{
for (int i=;i<;++i){
inque[i]=false;
price[i]=;
cnt[i]=;
}
}
bool spfa(int x)
{
queue<int>q;
cnt[x]++;
price[x]=1.0;
inque[x]=true;
q.push(x);
while (!q.empty()){
int now=q.front();
q.pop();
inque[now]=false;
for (int i=;i<g[now].size();++i){
int e=g[now][i];
int nxt=edge[e].v;
if (price[nxt]<price[now]*edge[e].rate){
price[nxt]=price[now]*edge[e].rate;//松弛
if (!inque[nxt]){
inque[nxt]=true;
q.push(nxt);
if (++cnt[nxt]>in_degree[nxt]){//某点的入队次数大于它的入度
return true;
}
}
}
}
}
return false;
}
int main()
{
//freopen("de.txt","r",stdin);
int casee=;
while (~scanf("%d",&n)){
if (n==) break;
money.clear();
for(int i=;i<;++i)
g[i].clear();
for (int i=;i<;++i)
in_degree[i]=;
for (int i=;i<n;++i){
string mny;
cin>>mny;
money[mny]=i;
}
scanf("%d",&m);
for (int i=;i<m;++i){
string a,b;
double x;
cin>>a>>x>>b;
edge[i].u=toNum(a);
edge[i].v=toNum(b);
edge[i].rate=x;
g[edge[i].u].push_back(i);
in_degree[edge[i].v]++;
}
bool ok=false;
for (int i=;i<n;++i){//从每个点跑spfa
init();//记得每次初始化
if (spfa(i)){
ok=true;
break;
}
}
if (ok)
printf("Case %d: Yes\n",++casee);
else
printf("Case %d: No\n",++casee);
}
return ;
}

这题813ms过的...folyd好像只要几十ms,就当练习spfa了。

												

POJ 2240 Arbitrage (spfa判环)的更多相关文章

  1. POJ 2240 Arbitrage spfa 判正环

    d[i]代表从起点出发可以获得最多的钱数,松弛是d[v]=r*d[u],求最长路,看有没有正环 然后这题输入有毒,千万别用cin 因为是大输入,组数比较多,然后找字符串用strcmp就好,千万不要用m ...

  2. POJ 2240 Arbitrage(判正环)

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

  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 1860 Currency Exchange【SPFA判环】

    Several currency exchange points are working in our city. Let us suppose that each point specializes ...

  5. 最短路(Floyd_Warshall) POJ 2240 Arbitrage

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

  6. poj 2240 Arbitrage 题解

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

  7. 2018.09.09 poj2949Word Rings(01分数规划+spfa判环)

    传送门 这题要先巧妙的转化一下. 对于每个字符串,我们把头尾的两个小字符串对应的点连边,边权是这个字符串的长度. 这样最多会出现26*26个点. 这个时候就只用求出边权和跟边数的最大比值了. 这个显然 ...

  8. 【BZOJ 3232】圈地游戏 二分+SPFA判环/最小割经典模型

    最小割经典模型指的是“一堆元素进行选取,对于某个元素的取舍有代价或价值,对于某些对元素,选取后会有额外代价或价值”的经典最小割模型,建立倒三角进行最小割.这个二分是显然的,一开始我也是想到了最小割的那 ...

  9. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

随机推荐

  1. pytest_用例运行级别_模块级

    ''' pytest 参数说明 https://www.jianshu.com/p/7a7432340f02 -x test_fixt_model.py 遇到错误时,停止运行 用-v运行(-v显示运行 ...

  2. (三)修改内核大小,适配目标板Nand flash分区配置

    一. 修改内核大小 1. 在你的配置文件下uboot/include/config/xxx.h 里面有一个宏定义 #define MTDPARTS_DEFAULT "mtdparts=jz2 ...

  3. JVM调优(三)——基于Btrace的监控调试

    JVM调优(三)--基于Btrace的监控调试 简介 Btrace可以动态地向目标应用程序的字节码注入追踪代码 用到的技术: JavaComplierApi.JVMTI.Agent.Instrumen ...

  4. JS-iframe高度自适应内容

    iframe:https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/iframe iframe 内容加载后不改变 使用:onload=&q ...

  5. The SDK directory '/home/wangju/gitProject/Automation/D:\Android_SDK' does not exist.

    执行gradle clean命令报错 gradle clean FAILURE: Build failed with an exception. * What went wrong: A proble ...

  6. ReplicatorLayer 复制图层

    使用文档介绍: #import <QuartzCore/CALayer.h> NS_ASSUME_NONNULL_BEGIN CA_CLASS_AVAILABLE (10.6, 3.0, ...

  7. 通过TCP/IP连接Mysql数据库

    问题:mysql只能用localhost或127.0.0.1连接 解决:mysql安装完后,默认是root用户,root用户只能在服务器登录,需要分配新用户. 1.以root用户登陆mysql数据库. ...

  8. HttpServletRequest 对文件上传的支持

    此前,对于处理上传文件的操作一直是让开发者头疼的问题,因为 Servlet 本身没有对此提供直接的支持,需要使用第三方框架来实现,而且使用起来也不够简单.Servlet 3.0 已经提供了这个功能,而 ...

  9. 【怒转】 idea快捷键说明大全(中英文对照)

    1 编辑[Editing] 快捷键 英文说明 中文说明 Ctrl + Space Basic code completion (the name of any class, method or var ...

  10. Python做个小游戏

    Ps.可去知乎搜索“雨露浅歌”大神,他写的帖子里有详细讲解和源码. 游戏概述.玩法:通过键盘的↑键来控制小球往上走,当松开↑键时,小球以一定速度向下掉,小球每越过一根棒加1000分,越过一个飞镖加20 ...