Currency Exchange
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 //n种货币,m个兑换点,num此时拥有第num种货币,money拥有第num种的金额 1 2 1.00 1.00 1.00 1.00 //a和b兑换,a换b利率为Rab,花费Cab,b换a利率为Rba,花费为Rba 2 3 1.10 1.00 1.10 1.00 Sample Output YES Source
Northeastern Europe 2001, Northern Subregion
|
题目大意:
换钱问题,换钱满足的式子为 (金额-花费)*利率
如果最后换回来的时候手里的钱比刚开始多 就输出 YES 否则 输出 NO
思路:因为可能出现负值,所以这里用bellman-ford算法,不过这里要进行一下变形。。。
bellman-ford算法模板:
/*Bellman-ford*/
for( int i = 0;i < n;i++ )
{
d[i] = INF;
}
d[0] = 0;
for( int j = 0;j < n-1;j++ )
{
for( int i = 0;i < m;i++ )
{
int x = u[i], y = v[i];
if( d[x] < INF )
d[y] = min(d[y], d[x]+w[i]);
}
}
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int N = 110;
int u[N*2], v[N*2]; //存放交换种类
double w[N*2]; //存放第i种钱的值
int n, m, num;
double money;
double r[N*2][N*2], c[N*2][N*2]; //分别存放利率和花费
bool bellman()
{
int j;
while( w[num]<=money ) //一直处理到金额变多为止
{
j = 1; //标志变量
for( int i = 0;i < 2*m;i++ ) //从第一个兑换点的情况开始遍历,每种包括 换过去 在换回来 所以是i++,而不是i = i + 2;
{
int x = u[i], y = v[i]; //暂存 方便使用
if( (w[x]-c[x][y])*r[x][y]>w[y] ) //计算 如果交换 所能得到的钱是否增加 若增加 则更新
{
w[y] = (w[x]-c[x][y])*r[x][y];
j = 0;
}
}
if( j ) return false; //如果遍历完了 j 没变 说明 金额不可能增加 所以返回 false
}
return true;
}
int main()
{
cin>>n>>m>>num>>money;
for( int i = 0;i < n;i++ ) //初始化0
w[i] = 0;
w[num] = money; //刚开始的金额
for( int i = 0;i < m*2;i = i+2 ) //2*m的原因为 需要存放a->b 和 b->a 的 种类 利率 以及金额
{
int a, b;
cin>>a>>b;
u[i] = a;
v[i] = b;
u[i+1] = b;
v[i+1] = a;
cin>>r[a][b]>>c[a][b]>>r[b][a]>>c[b][a];
}
if( bellman() ) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}