poj1860(Bellman—fold)

本文解决了一个经典的货币兑换问题,通过构建无向图并运用Bellman-Ford算法来判断是否可以通过一系列兑换操作使得初始资金增加。详细介绍了问题背景、输入输出格式及样例。

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

题目连接:http://poj.org/problem?id=1860

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

题目大意:有若干种货币,若干个兑换点,每个兑换点可以把一种货币兑换为另一种货币(可A->B,也可B->A),但是兑换有佣金,假设把A变为B,汇率为r,佣金为c,则B=(A-c)*r。给出这些兑换点的信息 以及 初始的钱的种类和数量,求是否可能进过若干次兑换使钱(最后必须是最开始的币种)变多;
解题思路:转化为图,货币为节点,兑换点为边,则构成一个无向图,而问题就转化成了求次无向图是否存在正环(因为最后要化成开始的币种,而不是价值变多即可,所以是求正环)
用Bellman——fold算法的思想,可以无限松弛即为正环,就可以解决了(原算法为求负环,只需把初始化的状态和松弛条件改一下即可)

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>

using namespace std;

struct Edge
{
    int from,to;
    double r,c;
    Edge(int u,int v,double r,double c):from(u),to(v),r(r),c(c) {}
};

vector<int> G [205];
vector<Edge> edges;
int cnt[105]= {0};
bool inq[105]= {0};
double d[105];
int n;

bool bellman_fold(int s,double value)
{
    queue<int> Q;
    memset(d,0,sizeof(d));
    Q.push(s);
    d[s]=value;
    inq[s]=1;
    while(!Q.empty())
    {
        int u=Q.front();
        Q.pop();
        inq[u]=0;
        for(int i=0; i<G[u].size(); i++)
        {
            int now=G[u][i];
            Edge & e=edges[now];
            if(d[u]>0&&d[e.to]<(d[u]-e.c)*e.r)
            {
                d[e.to]=(d[u]-e.c)*e.r;
                if(!inq[e.to])
                {
                    Q.push(e.to);
                    inq[e.to]=1;
                    if(++cnt[e.to]>n)
                        return 0;
                }
            }
        }
    }
    return 1;
}

int main()
{
    int m,no;
    int x=0;
    double sum;
    cin>>n>>m>>no>>sum;
    while(m--)
    {
        int no1,no2;
        double rab,cab,rba,cba;
        scanf("%d%d%lf%lf%lf%lf",&no1,&no2,&rab,&cab,&rba,&cba);
        edges.push_back(Edge(no1,no2,rab,cab));
        G[no1].push_back(x);
        x++;
        edges.push_back(Edge(no2,no1,rba,cba));
        G[no2].push_back(x);
        x++;
    }
    bool flag = bellman_fold(no,sum);
    if(flag)
        cout<<"NO"<<endl;
    else
        cout<<"YES"<<endl;
}

 

转载于:https://www.cnblogs.com/cryingrain/p/8677973.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值