POJ 1860 Currency Exchange

来源:互联网 发布:制作假营业执照软件 编辑:程序博客网 时间:2024/05/29 12:21
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.01 2 1.00 1.00 1.00 1.002 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-7int 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");elseprintf("NO\n");return 0;}


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

代码如下:

#include<iostream>#include<cstdio>using namespace std;#define MAX 0x3f3f3f3f#define N 105#define eps 1e-7int 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");elseprintf("NO\n");return 0;}


 

 

0 0
原创粉丝点击