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. pyCharm和解释器下载安装

    参考:(mac) 安装流程和注意: http://blog.csdn.net/limin2928/article/details/69267184 解释器下载地址: https://www.pytho ...

  2. CSS中的flex布局

    1.flex 布局的概念 Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.任何一个容器都可以指定为 Flex 布局,行内元素也可以通过 ...

  3. Thinkphp5 使用爬虫框架 QueryList3 的非composer方法教程

    首先,我要吐槽一下.我查了很多博客用tp框架使用composer方法结合 QueryList的教程.就是没有找到非composer的方法.于是摸索总结成功的经验: 工具:phpQuery.php 和 ...

  4. java高并发解决思路

    一个小型的网站,比如个人网站,可以使用最简单的html静态页面就实现了,配合一些图片达到美化效果,所有的页面均存放在一个目录下,这样的网站对系统架构.性能的要求都很简单,随着互联网业务的不断丰富,网站 ...

  5. 使用Echarts中遇到值得记录的小案例(一)

    需求部分 在开发项目的时候遇到一个需求,就是如何保证echarts图表里至少显示一个图例的数据(也就是最后一个图例不能变成unselected的状态)下图是最初加载时的画面 不想出现图例都被点击取消导 ...

  6. H5rem

    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1, ...

  7. .eslintrc.js相关配置

    module.exports = { root: true, //此项是用来指定javaScript语言类型和风格,sourceType用来指定js导入的方式,默认是script,此处设置为modul ...

  8. Vue.config.silent = true

    Vue.config   vue的全局配置文件 silent默认值是false Vue.config.silent = true 取消Vue所有的日志和警告

  9. android SharedPreferences 存储文件

  10. android中返回数据给上一个活动,可以用来回显数据

    (一)who简介:没错,就是startActivityForResult()方法,这个方法用来在活动被销毁的时候返回数据给上一个方法.参数说明: startActivityForResult(inte ...