Currency Exchange
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 37892 | Accepted: 14532 |
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
Source
Northeastern Europe 2001, Northern Subregion
问题链接:POJ1860 Currency Exchange
问题描述:有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币 交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到 (100 – 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加。首先输入三个整数n,m,s和一个实数v分别表示有n种货币(从1开始编号),有m个兑换点,现在有编号为s的货币价值为v。接着输入m行,每行用两个整数a,b和四个实数Rab,Cab,Rbc,Cba描述一个兑换点,分别表示此对换点实现编号为a和b的货币的兑换。由a兑换为b时的汇率和手续费分别为Rab和Cab;由b兑换为a时的汇率和手续费分别为Rba和Cba;
解题思路:由于可以重复兑换,因此只要存在一个正环,那么本金就会不断增加,因此实际上就是判读是否存在正环。Bellman – ford算法是求含负权图的单源最短路径的一种算法,效率较低,代码难度较小。其原理为连续进行松弛,在每次松弛时把每条边都更新一下,若在n-1次松弛后还能更新,则说明图中有负环,因此无法得出结果,否则就完成。Bellman_ford算法是判断是否存在负环,但是只要简单修改以下松弛条件和初始化,就能使用Bellman_ford算法判断是否存在正环。具体看代码实现
AC的C++程序:
#include<iostream>
#include<cstring>
using namespace std;
const int N=105;
struct Node{
int start,end;
double c,r;
}edge[2*N];
int n,m,M;
double dist[N];
//判断是否有正环,存在返回true,否则返回false
bool Bellman_ford(int s,double v)
{
memset(dist,0,sizeof(dist));
dist[s]=v;
for(int i=1;i<n;i++)//进行n-1次松弛
for(int j=0;j<M;j++)
if(dist[edge[j].end]<(dist[edge[j].start]-edge[j].c)*edge[j].r)
dist[edge[j].end]=(dist[edge[j].start]-edge[j].c)*edge[j].r;
//经过n-1次松弛操作后,如果还能松弛,说明存在正环
for(int j=0;j<M;j++)
if(dist[edge[j].end]<(dist[edge[j].start]-edge[j].c)*edge[j].r)
return true;
return false;
}
int main()
{
int s,a,b;
double v;
while(~scanf("%d%d%d%lf",&n,&m,&s,&v)){
M=0;
for(int i=0;i<m;i++){
scanf("%d%d",&a,&b);
edge[M].start=a,edge[M].end=b;
scanf("%lf%lf",&edge[M].r,&edge[M].c);
M++;
edge[M].start=b,edge[M].end=a;
scanf("%lf%lf",&edge[M].r,&edge[M].c);
M++;
}
if(Bellman_ford(s,v))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}