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. php min()函数 语法

    php min()函数 语法 作用:从所有参数中找到最小数 语法:min(X,Y,Z) 或者min(array(X,Y,Z)) 参数:min函数中参数至少一个,可以多个参数,也可以是数组. 说明:如果 ...

  2. PHP数据如何向上取整

    PHP数据如何向上取整? PHP数据向上取整可以通过ceil()函数来实现,ceil()函数表示向上舍入为最接近的整数. 语法是: 1 ceil(x) 参数 x 必需.一个数. 说明 返回不小于 x ...

  3. SDUT 1266 出栈序列统计(卡特兰数)

    这道题是回溯算法,网上一查是卡特兰数先占上代码,题解过两天会写. #include <bits/stdc++.h> using namespace std; int main() { // ...

  4. 2018-2019-2 实验四 Android程序设计

    实验要求 参考Android开发简易教程 完成云班课中的检查点,也可以先完成实验报告,直接提交.注意不能只有截图,要有知识点,原理,遇到的问题和解决过程等说明.实验报告中一个检查点要有多张截图. 发表 ...

  5. 如果遇到找不到元素如何处理? Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"investmentframe"}

    常见几种原因与应对,详细参见http://www.blogjava.net/qileilove/archive/2014/12/11/421309.html 1,动态ID无法找到,用xpath路径解决 ...

  6. linux-centOS环境下安装jdk8

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lx_Frolf/article/det ...

  7. python 将图片存入mongodb,读取图片,gridfs模块

    导入图片引入模块,其中gridfs模块不需要单独安装,引入了pymongo即可直接引入from pymongo import MongoClientfrom gridfs import *import ...

  8. Tex与PDF

    由Knuth Donald开发的tex.web会生成DVI文件,DVI也是Knuth自己实现的(虽然概念是其他人提出的)一种文件格式,目标是与设备无关. 通过dvips程序可以将DVI格式转化成Pos ...

  9. Mycat+Pxc的配置

    1 schema.xml配置文件 Balance属性 负载均称类型 0:不开启读写分离机制,所有读操作都发送到当前可用的writeHost上 1:全部的readHost与stand by writeH ...

  10. 对象与json字符串相互转化

    在java编程中,json字符串和对象的相互转化十分常用,下面我们就对象如何转化为json字符串以及json字符串如何转化为对象进行简要介绍,以便在代码中能方便使用. 1.依赖 本次介绍的方法依赖ja ...