zoukankan      html  css  js  c++  java
  • Currency Exchange POJ

    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
    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个兑换点,S开始的货币类型,V开始拥有的货币数
    给出M个兑换点的信息
    a b 代表兑换的两种货币,然后给出a兑换b的汇率和佣金,b兑换a的汇率和佣金
    问是否可以经过多轮兑换后使得本金变多

    思路:spfa判断正环
    两种方法 cnt【y】++ ,直到cnt【y】 >= n (y代表放入队列的点,也就是松弛点)
        cnt【y】+=cnt【x】,直到cnt【y】>=n
    当然这题也可以判断dist【v】 > V?(表示起点)
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<queue>
     4 
     5 using namespace std;
     6 
     7 int n,m,s;
     8 double v;
     9 
    10 struct Node
    11 {
    12     int y;
    13     double val;
    14     double sub;
    15     int next;
    16     Node(int y=0,double val=0,double sub = 0,int next=0):y(y),val(val),sub(sub),next(next) {}
    17 } node[205];
    18 
    19 int cnt,head[105];
    20 void add(int x,int y,double val,double sub)
    21 {
    22     node[++cnt].y=y;
    23     node[cnt].next=head[x];
    24     node[cnt].val=val;
    25     node[cnt].sub=sub;
    26     head[x]=cnt;
    27 }
    28 queue<int>que;
    29 double dist[105];
    30 int vis[105];
    31 int tot[105];
    32 int num[105];
    33 bool spfa()
    34 {
    35     while(!que.empty())que.pop();
    36     memset(vis,0,sizeof(vis));
    37     memset(dist,0,sizeof(dist));
    38     memset(num,0,sizeof(num));
    39     que.push(s);
    40     dist[s] = v;
    41     while(!que.empty())
    42     {
    43 
    44         int from = que.front();
    45         que.pop();
    46         vis[from] = 0;
    47         for(int i=head[from]; i; i=node[i].next)
    48         {
    49             int to = node[i].y;
    50             if(dist[to] < (dist[from]-node[i].sub)*node[i].val)
    51             {
    52                 dist[to] =  (dist[from]-node[i].sub)*node[i].val;
    53                 if(!vis[to])
    54                 {
    55                     que.push(to);
    56                     vis[to]=1;
    57                     num[to]++;
    58                     if(num[to]>= n)return 1;
    59                 }
    60             }
    61         }
    62        // if(dist[s] > v)return 1;
    63     }
    64     return 0;
    65 }
    66 
    67 int main()
    68 {
    69     scanf("%d%d%d%lf",&n,&m,&s,&v);
    70     for(int i=1; i<=m; i++)
    71     {
    72         int u,v;
    73         double a,b,c,d;
    74         scanf("%d%d",&u,&v);
    75         scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
    76         add(u,v,a,b);
    77         add(v,u,c,d);
    78     }
    79     int ans =  spfa();
    80     if(ans)printf("YES
    ");
    81     else printf("NO
    ");
    82 }
    View Code


  • 相关阅读:
    2019杭电多校训练(一)
    2019牛客多校训练(二)
    2019牛客多校训练(一)
    codeforces#1196F. K-th Path(最短路,思维题)
    codeforces#1108E2. Array and Segments (线段树+扫描线)
    codeforces#1183F. Topforces Strikes Back(数论)
    SPOJ
    2020年4月 第十一届蓝桥杯大赛个人赛(软件类)省级模拟赛
    Codeforces Round #634 (Div. 3)
    POJ
  • 原文地址:https://www.cnblogs.com/iwannabe/p/10772465.html
Copyright © 2011-2022 走看看