poj_1860Currency Exchange

Currency Exchange
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 15768   Accepted: 5447

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 RAB, CAB, RBA and CBA - 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<=103
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102
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 104

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
Description 
一些货币兑换点正在我们的城市工作。我们假设每个兑换点尤其擅长兑换两种特定的货币,并且只进行对这些货币的兑换。可能有一些兑换点专门兑换相同的货币对。每个兑换点都有自己的汇率,货币A到货币B的汇率是你用1货币A换到的货币B的数量。每个兑换点也有一些佣金,即为你需要为你的兑换行动支付的金额。佣金总是从源货币扣除。
例如,如果你想要在汇率为29.75,并且佣金为0.39的兑换点将100美元兑换成俄罗斯卢布,你将会得到(100-0.39)*29.75=2963.3975卢布。
你有N种不同的货币可以在我们的城市进行兑换。我们为每一种货币制定从1到N的唯一一个整数。每个兑换点可以用6个数字形容:整数A和B——它交换的货币(表示为货币A和货币B);实数RAB,CAB,RBA和CBA——当它分别将货币A兑换成货币B和将货币B兑换成货币A时的汇率和佣金。
叫兽有一定数量的货币S,并且它希望它能以某种方式在一些兑换行动后增加它的资本。最后它的资金必须兑换为货币S。
帮助它解决这个棘手的问题。叫兽在进行它的兑换行动时资金总数必须始终非负。
Input 
输入的第一行包含四个数字:N – 货币的数量,M – 兑换点的数量,S – 叫兽具有的货币种类,V – 叫兽具有的货币数量。
下面的M行每行包括6个数字 – 对相应的兑换点的描述。数字相隔一个或多个空格。1<=S<=N<=100,1<=M<=100,V是实数,0<=V<=10^3。
对于每个兑换点汇率和佣金都是实数,小数点后最多有两位小数。10^-2<=汇率<=10^2,0<=佣金<=10^2。
如果在一组兑换行动中没有兑换点被超过一次地使用,我们认为这组兑换行动是简单的。你可以认为最终数值和最初任何一个简单的兑换行动组的比例小于10^4。
Output 
如果叫兽能增加它的财产,输出YES,否则输出NO。
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
我们知道bellmanFord可以用来求有无负环,用它来求最短路还是很少见的,(一般都是使用SPFA)毕竟效率略低。反过来想,求最大环,然后与货币量比较
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#pragma warning(disable : 4996)
const int MAXN = 1001;

typedef struct Node
{
	int u, v;
	double r, c;
}Node;
int n, m;
double value;
Node edge[MAXN];
double dist[MAXN];
int eg;

bool bellman(int s)
{
	memset(dist, 0, sizeof(dist));
	int i;
	bool flag = false;
	dist[s] = value;
	while(dist[s] <= value)
	{
		flag = false;
		for(i = 0; i <= eg; i++)
		{
			if(dist[edge[i].v] < (dist[edge[i].u] - edge[i].c) * edge[i].r)
			{
				dist[edge[i].v] = (dist[edge[i].u] - edge[i].c) * edge[i].r;
				flag = true;
			}
		}
		if(!flag)
			return dist[s] > value;
	}
	return true;
}
int main()
{
	freopen("in.txt", "r", stdin);
	int i;
	int a, b, s;
	double rab, cab, rba ,cba;
	while(scanf("%d %d %d %lf", &n, &m, &s, &value)!=EOF)
	{
		eg = 0;
		for(i = 0; i < m; i++)
		{
			scanf("%d %d %lf %lf %lf %lf", &a, &b, &rab, &cab, &rba, &cba);
			edge[eg].u = a;
			edge[eg].v = b;
			edge[eg].r = rab;
			edge[eg].c = cab;
			eg++;
			edge[eg].u = b;
			edge[eg].v = a;
			edge[eg].r = rba;
			edge[eg].c = cba;
			eg++;
		}
		if(bellman(s))
		{
			printf("YES\n");
		}
		else
		{
			printf("NO\n");
		}
	}
	return 0;
}


原文地址:https://www.cnblogs.com/lgh1992314/p/5834993.html