poj-1860 Currency Exchange (货币转换—最长路)

Currency Exchange
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 31527 Accepted: 11974

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.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种货币转换关系,两种货币之间有个固定的汇率rate,每次转换的时候需要支付一定的佣金才可以成功完成货币的转换。例如题干中给的例子,US dollar 和 Russian Rubles 之间的汇率 为 29.75,每次转换所需要花费的佣金是0.39,所以你用100 US dollar 所能 换得的Russian Rubles 为 (100 - 0.39)*29.75 = 2963.3975。现在题目所问的是一开始如果给你第s种货币总共v元,你是否能够通过已知的汇率去转换货币最后获得超过v元的第s种货币。

题目思路:本题可以看做在求最长路,两种货币转换的汇率看做边的权值,每次都是将更新更大的权值。解题的话将s做为原点,从s点出发能否出现再次回到s点时权值大于原来的v即可。


AC代码如下:

#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define FIN freopen("in.txt","r",stdin)
#define fuck(x) cout<<'['<<x<<']'<<endl
using namespace std;
typedef long long LL;
typedef pair<int,int>pii;
const int MX = 100 + 10;

int n,m,s;
double v;
double rate[MX][MX],cost[MX][MX];//由于图较小,可通过邻接矩阵来储存a到b的汇率以及所需花费的佣金;

int vis[MX];
double w[MX];

bool spfa(int s){
    for(int i = 0;i <= n;i++) w[i] = 0;
    memset(vis,0,sizeof(vis));

    queue<int>q;
    w[s] = v;//将初始的权值初始化为v;
    q.push(s);

    while(!q.empty()){
        int u = q.front();q.pop();
        vis[u] = 0;

        for(int i = 1;i <= n;i++){
            if(w[i] < (w[u] - cost[u][i])*rate[u][i]){//如果出现比原来更高的价值就更新当前点;
                w[i] = (w[u] - cost[u][i])*rate[u][i];
                if(w[s] > v)//和普通的spfa唯一不同的点就是在这加一个判断,如果刚好在s点更新之后出现了大于v的权值就返回true;
                    return true;
                if(!vis[i]){
                    vis[i] = 1;
                    q.push(i);
                }
            }
        }
    }
    return false;//否则返回false;
}

int main(){
    while(~scanf("%d%d%d%lf",&n,&m,&s,&v)){
        for(int i = 0;i <= n;i++){
            for(int j = 0;j <= n;j++){
                if(i == j) rate[i][j] = 1;
                else rate[i][j] = 0;
                cost[i][j] = 0;
            }
        }
        int a,b;
        double rab,rba,cab,cba;
        for(int i = 0;i < m;i++){
            scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
            rate[a][b] = rab;
            rate[b][a] = rba;
            cost[a][b] = cab;
            cost[b][a] = cba;//构建邻接矩阵;
        }
        if(spfa(s))
            puts("YES");
        else
            puts("NO");
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值