poj1860 - Currency Exchange

来源:互联网 发布:淘宝买家签收规则 编辑:程序博客网 时间:2024/06/07 17:51

题目网址:http://poj.org/problem?id=1860

题目:

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 23979 Accepted: 8696
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

题目大意:
有一个孩子,就是想要换钱嘛,两个地方的钱的汇率不一样,然后换钱还需要押金。
所以那个孩子就想,会不会有那么一种情况,换着换着钱就多了起来。

前面四个数字是n(货币的数量), m(有m个交换点), s(开始拥有的货币编号),以及money
最后两个数字表示换钱的地点,以及从第一个地点换到第二个地点的汇率已经佣金,还有第二个地点到第一个地点换钱的汇率和佣金。

思路:
看看是不是有正环,这就是最短路的问题了,直接用bellman算法松弛就好了。看看是不是有正环,因为最后的金币得和原来的金币是同一种币种呀

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAX = 110;int n, m, s;double monney;struct Point{    int u, v;//从u换到v    double rate, comm;//汇率、佣金} point[2*MAX];double dist[MAX];bool Bell_man(){    for(int i = 0; i <= n; i++)//初始化    {        dist[i] = 0.0;    }    dist[s] = monney;//最开始拥有的钱    for(int j = 1; j < n; j++)//最多进行n-1次松弛    {        bool flag = true;        for(int i = 0; i < 2*m; i++)        {            if(dist[point[i].v] < (dist[point[i].u]-point[i].comm)*point[i].rate)//表示可以松弛(也就是钱变多了)            {                dist[point[i].v] = (dist[point[i].u]-point[i].comm)*point[i].rate;                flag = false;            }        }        if(flag)//不能再松弛了,所以就一定没有正环了,直接返回不能        {            return 0;        }    }    for(int i = 0; i < 2*m; i++)//判断是不是有正环    {        if(dist[point[i].v] < (dist[point[i].u]-point[i].comm)*point[i].rate)        {            return 1;        }    }    return 0;}int main(){    while(scanf("%d%d%d%lf", &n, &m, &s, &monney) != EOF)    {        int a, b, k = 0;        double c, d, c1, d1;        for(int i = 1; i <= m; i++)//双向兑换        {            scanf("%d%d%lf%lf%lf%lf", &a, &b, &c, &d, &c1, &d1);            point[k].u = a,            point[k].v = b;            point[k].rate = c;            point[k++].comm = d;            point[k].v = a;            point[k].u = b;            point[k].rate = c1;            point[k++].comm = d1;        }        if(Bell_man())        {            printf("YES\n");        }        else        {            printf("NO\n");        }    }    return 0;}
0 0
原创粉丝点击