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. Java Web学习总结(9)学习总结-JSTL

    一,JSTL概述 JSTL(JSP Standard Tag Library),JSP标准标签库,可以嵌入在jsp页面中使用标签的形式完成业务逻辑等功能.jstl出现的目的同el一样也是要代替jsp页 ...

  2. Codeforces 842C--Ilya And The Tree(dfs+树)

    原题链接:http://codeforces.com/contest/842/problem/C 题意:一个以1为根节点的树,每个节点有一个值ai,定义美丽度:从根节点到这个节点的路径上所有ai的gc ...

  3. 540D - Bad Luck Island(概率DP)

    原题链接:http://codeforces.com/problemset/problem/540/D 题意:给你石头.剪刀.布的数量,它们之间的石头能干掉剪刀,剪刀能干掉布,布能干掉石头,问最后石头 ...

  4. [CSP-S模拟测试]:biology(DP)

    题目传送门(内部题23) 输入格式 第一行有$2$个整数$n,m$.接下来有$n$行,每行$m$个整数,表示$a$数组.接下来有$n$行,每行$m$个整数,表示$b$数组. 输出格式 一行一个整数表示 ...

  5. 老牌激活工具– Microsoft Toolkit 2.4.3 + 详细图文教程【转】

    老牌激活工具-- Microsoft Toolkit 2.4.3 + 详细图文教程 windowsToolkit是一个一键激活MS Office 2010的工具.原理就是利用KMS来激活,不是新的激活 ...

  6. js中slice、splice、substr、split方法

    1.slice 可用于数组与字符串,返回一个新的数组,原数组不改变,包含从 start 到 end (不包括该元素)的 arrayObject 中的元素. 在string中 slice(start,e ...

  7. Ionic4 入门

    1.搭建环境 1.电脑安装node.js,安装后电脑会自动安装npm     2.通过cmd命令,安装cnpm npm install -g cnpm -registry=https://regist ...

  8. Kindeditor在线文本编辑器过滤HTML

    KindEditor.ready(function (K) { editor = K.create('textarea[name="content"]', { filterMode ...

  9. Python for Eclipse插件Pydev安装后eclipse里面看不到

    反复安装几次在eclipse里面都找不到,崩溃了,有的说是pydev版本不对,应该和python版本一致,都是扯淡,去pydev官网看了下requirement原来是需要Java1.7+以上,安装1. ...

  10. this关键字与super关键字区别

        this super 1 访问属性 访问本类中属性,如果本类中没有此属性,就从父类继承过来的属性中查找 (遵循就近原则) 访问父类中的属性 2 调用方法 访问本类中方法 直接访问父类中方法 3 ...