POJ 1860 Currency Exchange(Bellman Ford算法)

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37618760/article/details/82186314

Currency Exchange

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 36915   Accepted: 14172

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

Source

Northeastern Europe 2001, Northern Subregion

思路:首先是将题面翻译了一下:

例如,如果您想在兑换点兑换100美元兑换俄罗斯卢布,汇率为29.75,佣金为0.39,您将获得(100 - 0.39)* 29.75 = 2963.3975RUR。您肯定知道我们可以在我们的城市处理N种不同的货币。让我们为每种货币分配从1到N的唯一整数。然后每个交换点可用6个数字来描述:整数A和B - 它交换的货币数量,以及真实RAB,CAB,RBA和CBA - 分别交换A到B和B到A时的汇率和佣金。尼克有一些货币S的钱,并想知道他是否可以某种方式,在一些交易所操作后,增加他的资本。当然,他最终想要用货币S赚钱。帮助他回答这个棘手的问题。尼克在进行操作时必须总是有非负的金额。

这道题目的中心思路是这样的,只要有一个环,实现增加钱的目的,无限次循环这个环然后退出即可。

那么我们先贴上源代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=2000+10;
const int INF=1<<30;
int n,m,s;double v;//n代表货币数量  m代表交换点数量   s代表nick拥有的货币数量  v代表拥有的货币单位数量
struct Node{
    int s,e;//起点  终点
    double w,c;//汇率  佣金
    Node(){}
    Node(int s,int e,double w,double c):s(s),e(e),w(w),c(c){}//构造函数
}e[maxn];
double dist[maxn];
int tot;
bool Bellman_ford(int u,double s){
    memset(dist,0,sizeof(dist));
    dist[u]=s;//源点初始化为1
    //Bellman  Ford核心算法
    for(int i=0;i<n;i++){
        for(int j=0;j<tot;j++){
            int x=e[j].s,y=e[j].e;
            double r=e[j].w,c=e[j].c;
            if((dist[x]-c)*r>dist[y])
            {
                dist[y]=(dist[x]-c)*r;
                if(i==n-1) return true;//说明存在正环
            }
        }
    }
    return false;
}
int main(){
    int s,a,b;
    double r1,c1,r2,c2;
    while(cin>>n>>m>>s>>v){
        tot=0;
        for(int i=0;i<m;i++){
            cin>>a>>b>>r1>>c1>>r2>>c2;
            e[tot++]=Node(a,b,r1,c1);//用tot来控制交换的两点编号
            e[tot++]=Node(b,a,r2,c2);
        }
        if(Bellman_ford(s,v)) cout<<"YES"<<endl;
        else  cout<<"NO"<<endl;
    }
    return 0;
}

我们在第一行输入n,m,s,v  ,具体代表的含义在源代码的注释当中有。

接下来我们用一个for循环输入从0到m的各个数据,一共是6个

分别代表:起点A 终点B  A转B的汇率  A转B的佣金   B转A的汇率  B转A的佣金

 这以后每一组的4个数据就直接装进Node结构体当中构造的函数。

在最核心的函数Bellman_ford中,我们继承Bellman算法最基础的模板,然后进行松弛。假如松弛成功就要进行更新。

那么只要我们一直赚钱,操作就不会停止。最后发现只要是正环就返回真值。

实话讲,这种题目还是很舒服的,感觉比前面牵涉队列的题目容易一些。

猜你喜欢

转载自blog.csdn.net/qq_37618760/article/details/82186314