poj2240 Arbitrage (spfa判环)

Arbitrage

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 10997   Accepted: 4622

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

Source

Ulm Local 1996

其实题目的大意就是能不能通过各个国家汇率的不同赚到比当初更多的钱。

也就是spfa判环

#include <stdio.h>
#include <vector>
#include <string.h>
#include <queue>
using namespace std;
int n,m;
//邻接表
vector<int>map[33];
//存贮国家名字
char name[33][100];
//两个国家的汇率
double f[33][33];
//起点国家到当前国家转换后的货币量
double price[33];
//入度
int in_degree[33];
//进队列的次数
int sum[33];
//是否在队列
bool in_queue[33];
int tonum(char *str)
{
	for(int i=0;i<n;i++)
	if(strcmp(str,name[i])==0)
	return i;
}
bool spfa(int x)
{
	queue<int>s;
	sum[x]++;
	price[x]=1.0;
	in_queue[x]=true;
	s.push(x);
	while(!s.empty())
	{
		int pos=s.front();
		s.pop();
		in_queue[pos]=false;
		for(int i=0;i<map[pos].size();i++)
		{
			int edge=map[pos][i];
			if(price[edge]<price[pos]*f[pos][edge])
			{
				price[edge]=price[pos]*f[pos][edge];
				if(!in_queue[edge])
				{
					in_queue[edge]=true;
					s.push(edge);
					if(++sum[edge]>in_degree[edge])
					{
						return true;
					}
				}
			}
		}
	}
	return false;

}
int main()
{
	int t=0;
	while(~scanf("%d",&n))
	{
		if(n==0)
		break;
		memset(in_degree,0,sizeof(in_degree));
		memset(name,0,sizeof(name));
		memset(map,0,sizeof(map));
		memset(f,0,sizeof(f));
		for(int i=0;i<n;i++)
		scanf("%s",name[i]);
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			char str1[100],str2[100];
			memset(str1,0,sizeof(str1));
			memset(str2,0,sizeof(str2));
			double x;
			scanf("%s %lf %s",str1,&x,str2);
			int a=tonum(str1);
			int b=tonum(str2);
			f[a][b]=x;
			in_degree[b]++;
			map[a].push_back(b);
		}
		bool ok=false;
		for(int i=0;i<n;i++)
		{
			if(spfa(i))
			{
				ok=true;
				break;
			}
			memset(in_queue,false,sizeof(in_queue));
			memset(price,0,sizeof(price));
			memset(sum,0,sizeof(sum));
		}
		if(ok)
		printf("Case %d: Yes\n",++t);
		else
		printf("Case %d: No\n",++t);
	}
	return 0;
}
时间: 2024-11-15 09:49:57

poj2240 Arbitrage (spfa判环)的相关文章

POJ 2240 Arbitrage (spfa判环)

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 Frenc

hdu 1317 XYZZY(spfa判环)

http://acm.hdu.edu.cn/showproblem.php?pid=1317 大致题意:有n个房间,每个房间都有对应的能量值(可正可负),现在从1出发要到达n,初始能量为100,问是否能够达到n点,到达n的条件是中间及最后的能量值都要大于0. 思路:若不考虑环,那么求最长路判断是否大于0即可.若存在负环,对求最长路也没影响:但当存在正环时,最长路就不存在了.可用spfa判断,当某点入队超过n次,那么它必定在环中,直接将其dis置为INF,并不再将其近队列.最后若能到达n则可行,否

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

最小割经典模型指的是“一堆元素进行选取,对于某个元素的取舍有代价或价值,对于某些对元素,选取后会有额外代价或价值”的经典最小割模型,建立倒三角进行最小割.这个二分是显然的,一开始我也是想到了最小割的那个模型的但是我觉得他会不是一个圈我就否掉了,但是仔细想想的话会发现,如果是这样的话所得到的答案一定小于等于一个圈的答案(浓度),所以我们可定会得到最终答案,所以这样做是可以的,所以说要有宽松得正解的意识(泥沙俱下但沙子不影响我泥).当时我否掉最小割以后就立马去想费用流了,然后想到建图后发现那样建图虽

poj2240 最短路判环

题意:与poj1680一样,有不同的换钱渠道,可以完成特定两种货币的交换,并且有汇率,只不过此题是单向边,然后问是否能使财富增加 与poj1680一样,建图之后直接spfa判增值的环即可 1 #include<iostream> 2 #include<cstdio> 3 #include<string> 4 #include<cstring> 5 #include<queue> 6 #include<map> 7 #include&l

nyoj973 天下第一(spfa判环)

题目973 题目信息 运行结果 本题排行 讨论区 天下第一 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 AC_Grazy一直对江湖羡慕不已,向往着大碗吃肉大碗喝酒的豪情,但是"人在江湖漂,怎能 不挨刀","人在江湖身不由己",如果自己的武功太差,在江湖会死的很惨,但是AC_Grazy没有 武功秘籍练不了绝世武功.有道是"山重水复疑无路,柳暗花明又一村",在AC_Grazy家里面 竟然藏着一本书,书名竟然叫做[超级

poj 3259 Wormholes(spfa 判环)

#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #include<queue> using namespace std; #define INF 100000000 int u[6000],v[6000],w[6000]; int first[6000],next[6000]; int coun[6000]; __int64 d[6000]; in

poj1860(spfa判正环)

题目连接:http://poj.org/problem?id=1860 题意:有多种从a到b的汇率,在你汇钱的过程中还需要支付手续费,那么你所得的钱是 money=(nowmoney-手续费)*rate,现在问你有v钱,从s开始出发交换钱能不能赚钱. 分析:如何存在正环,能无限增加钱,肯定可以赚了,因此用spfa判一下即可 #include <cstdio> #include <cstring> #include <string> #include <cmath&

Poj3259--Wormholes(Spfa 判负环)

Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36836   Accepted: 13495 Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way p

POJ 1860——Currency Exchange——————【最短路、SPFA判正环】

Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1860 Description Several currency exchange points are working in our city. Let us suppose that each point specializes in two par