POJ 1860 Currency Exchange(Ballman)

探讨如何利用Bellman-Ford算法解决货币兑换问题,通过优化算法避免超时,实现货币价值最大化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Currency Exchange
Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%lld & %llu

 

Description

Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency. 
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR. 
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real R AB, C AB, R BA and C BA - exchange rates and commissions when exchanging A to B and B to A respectively. 
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations. 

Input

The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=10 3
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 10 4

Output

If Nick can increase his wealth, output YES, in other case output NO to the output file.

Sample Input

3 2 1 20.0
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

Sample Output

YES

 

这道题!!!竟然是我第一道写Bellman—Ford算法的题目!!!而且!!!这题目你叫我怎么看得出要用最短路来做啊,如果不是我是做专题训练,专题写着最短路,完全想不到啊!!!ACM真的博大精深。

这道题的题意就是输入的第一行有三个数字,分别是货币数目,兑换所数目,一开始拥有的货币型号,一开始拥有货币的数量

然后下面就是M个兑换所,可以兑换比如样例,第二个兑换所可以2 和 3 互换,2 换 3 汇率是1.10,佣金是1.00,3 换 2 汇率是1.10,佣金是1.00

假设有x元货币2,兑换货币3的时候计算公式是(x - 1.00(佣金))*1.10(汇率)。题目就是要你最终让一开始的拥有的货币型号数目比一开始拥有的货币数量多就行了。

这道题虽然用的是Bellman算法,但是其实是不关负权的问题的,最主要是Bellman算法里面记录边的结构体改一改很容易就出答案了,Dijkstra的话。。。Dijkstra对于起始点是不记录的,因为会把它标记为已经寻找过了的,但是Bellman算法不会,直接纯暴力,复杂度是O(V*E),也就是边的数量乘以点的数量。每次都一直在更新,没有说哪个点用过就标记然后不用了,所以起始点也是一直在更新的。

代码如下:

#include<iostream>
#include<cstdio>
using namespace std;

#define MAX 0x3f3f3f3f
#define N 105
#define eps 1e-7
int nodenum, edgenum, original; //点,边,起点

typedef struct Edge //边
{
	int u, v;
	double utv_rate,utv_com;
	double vtu_rate,vtu_com;
}Edge;

Edge edge[N];
double dis[N], ori_value;

bool Bellman_Ford()
{
	bool flag;
	int tmp = 10000;
	for(int i = 1; i <= nodenum; ++i) //初始化
		dis[i] = i == original ? ori_value : 0;
	while(tmp--)
	{
		flag = 1;
		for(int i = 1; i <= nodenum - 1; ++i)
		{			
			for(int j = 1; j <= edgenum; ++j)
			{
				if(eps < (dis[edge[j].u] - edge[j].utv_com) * edge[j].utv_rate - dis[edge[j].v])
				{
					flag = 0;
					dis[edge[j].v] = (dis[edge[j].u] - edge[j].utv_com) * edge[j].utv_rate;
					if(dis[original] - ori_value > eps)
						return true;
				}
				if(eps < (dis[edge[j].v] - edge[j].vtu_com) * edge[j].vtu_rate - dis[edge[j].u])
				{
					flag = 0;
					dis[edge[j].u] = (dis[edge[j].v] - edge[j].vtu_com) * edge[j].vtu_rate;
					if(dis[original] - ori_value > eps)
						return true;
				}
			}
		}
		if(flag)
			break;
	}
	return false;
}

int main()
{
	scanf("%d%d%d%lf", &nodenum, &edgenum, &original, &ori_value);
	for(int i = 1; i <= edgenum; ++i)
	{
		scanf("%d%d", &edge[i].u, &edge[i].v);
		scanf("%lf%lf", &edge[i].utv_rate, &edge[i].utv_com);
		scanf("%lf%lf", &edge[i].vtu_rate, &edge[i].vtu_com);
	}
	if(Bellman_Ford())
		printf("YES\n");
	else
		printf("NO\n");
	return 0;
}


 我发现,我在做POJ2240的时候,也是类似的题目,但是上面这种做法已经是行不通的了,会超时什么的,所以我想了想,其实上面的做法对于这道题目还是有道理的,当然前提是不超时,而且题目告诉你了如果盈利情况存在最多10000次肯定会高于一开始拥有的价值数量。但是POJ2240的题目并没有这方面的说明, 我直接用这个题目改一改是过不去的,所以我去看了看别人的代码,我觉得,别人的代码比我更好,更有道理,更巧妙,也不会怕超时。

代码如下:

#include<iostream>
#include<cstdio>
using namespace std;

#define MAX 0x3f3f3f3f
#define N 105
#define eps 1e-7
int nodenum, edgenum, original; //点,边,起点

typedef struct Edge //边
{
	int u, v;
	double utv_rate,utv_com;
	double vtu_rate,vtu_com;
}Edge;

Edge edge[N];
double dis[N], ori_value;

bool Bellman_Ford()
{
	bool flag;
	int tmp = 10000;
	for(int i = 1; i <= nodenum; ++i) //初始化
		dis[i] = i == original ? ori_value : 0;
	for(int i = 1; i <= nodenum - 1; ++i)
	{			
		for(int j = 1; j <= edgenum; ++j)
		{
			if((dis[edge[j].u] - edge[j].utv_com) * edge[j].utv_rate > dis[edge[j].v])
			{
				dis[edge[j].v] = (dis[edge[j].u] - edge[j].utv_com) * edge[j].utv_rate;
			}
			if((dis[edge[j].v] - edge[j].vtu_com) * edge[j].vtu_rate > dis[edge[j].u])
			{
				dis[edge[j].u] = (dis[edge[j].v] - edge[j].vtu_com) * edge[j].vtu_rate;
			}
		}
	}
	for(int j = 1; j <= edgenum; ++j)//如果有一个可以不断增加的环,就返回真,否则返回假
	{
		if((dis[edge[j].u] - edge[j].utv_com) * edge[j].utv_rate > dis[edge[j].v])
		{
			return true;
		}
		if((dis[edge[j].v] - edge[j].vtu_com) * edge[j].vtu_rate > dis[edge[j].u])
		{
			return true;
		}
	}
	return false;
}

int main()
{
	scanf("%d%d%d%lf", &nodenum, &edgenum, &original, &ori_value);
	for(int i = 1; i <= edgenum; ++i)
	{
		scanf("%d%d", &edge[i].u, &edge[i].v);
		scanf("%lf%lf", &edge[i].utv_rate, &edge[i].utv_com);
		scanf("%lf%lf", &edge[i].vtu_rate, &edge[i].vtu_com);
	}
	if(Bellman_Ford())
		printf("YES\n");
	else
		printf("NO\n");
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值