Currency Exchange (POJ-1860)

本文探讨了一个涉及货币兑换点的问题,通过使用Floyd算法判断是否存在正环,从而确定是否可以通过一系列的兑换操作增加初始资金。文章提供了详细的算法实现和代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Currency Exchange (POJ-1860)

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

题目翻译:

我们城市有几个货币兑换点。假设每个点都专门使用两种特定的货币,并且只使用这些货币执行交换操作。可以有几个点专门针对同一对货币。每个点都有自己的汇率,A到B的汇率是1A的B的数量。而且每个兑换点都有一定的佣金,也就是你必须为你的兑换业务支付的金额。佣金总是以来源货币收取。

例如,如果你想把100美元兑换成俄罗斯卢布,汇率是29.75,佣金是0.39,你会得到(100 - 0.39)* 29.75 = 2963.3975卢布。

您一定知道在我们这个城市有N种不同的货币可以兑换。让我们为每种货币分配从1到N的唯一整数。然后,每个汇兑点可以用6个数字来描述:整数A和B - it兑换货币的数量,以及实际R AB、C AB、R BA和C BA -分别兑换A和B时的汇率和佣金。

尼克有一些现金,他想知道,在做了一些外汇交易之后,他是否能以某种方式增加资本。当然,他最终还是想把钱换成货币。帮助他回答这个难题。尼克在做他的生意时,一定要有一笔非负数的钱。

输入

输入的第一行包含四个数字:N—货币数量,M—交换点的数量,S—Nick拥有的货币数量,V—他拥有的货币单位数量。下面的M行包含6个数字,每个数字表示对应的交换点,按上述顺序排列。数字由一个或多个空格分隔。1 < = S < = N < = 100, 1 < = M < = 100, V是实数,0 < = 10 V < = 3。

对于每一个点,汇率和佣金都是真实的,小数点后最多有两位数字,10 -2<=汇率<=10 2,0<=佣金<=10 2。

如果在这个序列中不止一次使用交换点,那么让我们将交换操作的一些序列称为simple。您可以假设,在交换操作的任何简单序列的末尾和开头,总和的数值之比将小于10 4。

输出

如果Nick可以增加他的财富,输出YES,在其他情况下输出NO到输出文件。

样例输入

3 2 1 20.0

1 2 1.00 1.00 1.00 1.00 1.00 1.00

2 3 1.10 1.00 1.10 1.00 1.10 1.00

样例输出

是的

思路:思路很简单,只要floyd判断是否有正环,那么循环无数次之后一定是可以赚回本钱的,至于有些人循环一圈之后就与本金比是否增大是不可取的,因为万一转为本金的佣金很高,但是存在一个一次增加一点点的正环的话就需要循环很多次才可以高于本金。

AC代码:

//Rab=a到b的汇率,Cab=a到b的佣金,Rba=b到a的汇率,Cba=b到a的佣金;
#include <iostream>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#define inf 0x3f3f3f3f
#define MAX 110
using namespace std;
double R[MAX][MAX]= {0}; //汇率
double C[MAX][MAX]= {0}; //佣金
double dis[MAX]= {0};
int n,m,s;
double v;
int floyd()
{
    int i,j,k;
    double d[MAX];
    for(int i=1; i<=n; i++)
        d[i]=dis[i];
    for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                if((dis[i]-C[i][j])*R[i][j]>dis[j])
                {
                    dis[j]=(dis[i]-C[i][j])*R[i][j];
                }
            }
    for(i=1; i<=n; i++)
    {
        if(d[i]<dis[i])
            return 1;
    }
    return 0;
}
int main()
{
    scanf("%d%d%d%lf",&n,&m,&s,&v);
    int i,j,k;
    int a,b;
    double o,p,q,r;
    for(i=1; i<=m; i++)
    {
        scanf("%d%d%lf%lf%lf%lf",&a,&b,&o,&p,&q,&r);
        R[a][b]=o,C[a][b]=p;
        R[b][a]=q,C[b][a]=r;
    }
    dis[s]=v;
    floyd();
    if(floyd())
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值