Currency Exchange POJ1860

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<=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
1 2 1.00 1.00 1.00 1.00
2 3 1.10 1.00 1.10 1.00

Sample Output

   YES

题意:

大概就是换钱,交换点是1~N,每个点都能把相应的钱转换为另一种,(本金-手续费)*汇率=转换后所得的钱。

问你能不能让他的钱变多。

思路:

要自闭了,这么难的英文,看半天都看不懂。。。。。N,M,S,V:分别代表N个交换点,m种转换方式,S是初始的货币种类(即与源点),V是拥有的S类货币数量,每行输入 A、B、RAB、CAB、RBA、CBA 分别为A、B两种货币,A换成B的汇率以及手续费,B换成A的汇率以及手续费。把题看为求源点到其他点的最短路径即可,用Bellman-ford判断是否有正环即可(因为是想让钱那边多)。

代码:

#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <limits>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define Sci(x) scanf("%d",&x)
#define Sci2(x, y) scanf("%d%d",&x,&y)
#define Sci3(x, y, z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%I64d",&x)
#define Scl2(x, y) scanf("%I64d%I64d",&x,&y)
#define Scl3(x, y, z) scanf("%I64d%I64d%I64d",&x,&y,&z)
#define Pri(x) printf("%d\n",x)
#define Prl(x) printf("%I64d\n",x)
#define For(i,x,y) for(int i=x;i<y;i++)
#define FFor(i,x,y) for(int i=x;i>y;i--)
#define For_(i,x,y) for(int i=x;i<=y;i++)
#define FFor_(i,x,y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define LL long long
#define ULL unsigned long long
#define MAXSIZE 255
#define INF 0x3f3f3f3f
const int mod = 1e9+7;
const double PI = acos(-1.0);

using namespace std;
struct node
{
    int s;
    int e;
    double r;
    double c;
} edge[MAXSIZE];
double dis[MAXSIZE];//dis[i]表示当货币种类为i时钱的数目
int n,m,s;
double v;//钟类,交换点  哪个  多少钱,
void bellman(int k)
{
    Mem(dis,0);//这个地方初始化为0,因为要判断是否有正环
    dis[s]=v;//源点距离为v即他最开始拥有的钱
    For(i,1,n)
    For(j,1,k)
    {
        node data=edge[j];
        if(dis[data.e]<(dis[data.s]-data.c)*data.r)//此处用的小于号,松弛找到做多的钱
            dis[data.e]=(dis[data.s]-data.c)*data.r;
    }
    For(i,1,k)
    {
        node data=edge[i];
        if(dis[data.e]<(dis[data.s]-data.c)*data.r  )//说明有正环
        {
            printf("YES\n");
            return ;
        }
    }
    printf("NO\n");
}
int main()
{
    // int x,y,z;
    //if(dis[edge[i].e]<dis[edge[]])
    //scanf("%d%d%d%d",)
    Sci3(n,m,s);
    scanf("%lf",&v);
    int s,e;
    int k=1;
    while(m--)
    {
        Sci2(s,e);
        edge[k].s=s;
        edge[k].e=e;
        scanf("%lf%lf",&edge[k].r,&edge[k].c);
        k++;
        edge[k].s=e;
        edge[k].e=s;
        scanf("%lf%lf",&edge[k].r,&edge[k].c);
        k++;
    }
    bellman(k);
    return 0;
}
View Code

 

猜你喜欢

转载自www.cnblogs.com/hbhdhd/p/11297954.html