Currency Exchange POJ1860 bellman_ford

Currency Exchange

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 27630 Accepted: 10257

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
BAand 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 种 currency, M种兑换方式,Nick 拥有的的currency 编号S 以及他的具体的currency(V)。M 种兑换方式中每种用6个数描述: A, B, Rab, Cab, Rba, Cba。其中,Rab: 货币A 兑换 货币B 的汇率为Rab,佣金为Cab。Rba:货币B 兑换 货币 A 的汇率,佣金为Cba。假设含有的A货币是x,那么如果兑换成B,得到的货币B 就是:(x-Cab) * Rab。问从 货币S 经过一定次数的兑换,最终回归到货币S,能否使得 Nick 本来含有的 S 大。

分析:其实仔细想想就知道,此题如果有一个正的回路就应该输出yes,当然这个正回路应该和起点相连接。在最短路中有一种算法是可以判断负圈的,在这里可以反着用,用bellman_ford算法判断正圈。。

代码如下:

<span style="font-size:18px;">#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxx=10005;
struct My{
int a;int b;
double hui,shui;
}cun[maxx];
double d[maxx];
int n,m;
double num;
int V=1;


void add_edge(int a,int b,double c,double d)
{
    cun[V].a=a;cun[V].b=b;
    cun[V].hui=c;cun[V++].shui=d;
}


bool bellman_floyd(int s){
memset(d,0,sizeof(d));
d[s]=num;
for(int i=1;i<=n;i++)
{
      bool flag=false;
    for(int j=1;j<=V;j++)
    {
      My mm=cun[j];
        if(d[mm.b]<(d[mm.a]-cun[j].shui)*cun[j].hui){
        d[mm.b]=(d[mm.a]-cun[j].shui)*cun[j].hui;flag=true;
        }
    }
    if(!flag)break;
}
for(int i=1;i<=V;i++)
 if(d[cun[i].b]<(d[cun[i].a]-cun[i].shui)*cun[i].hui)
return  false;
return true;
}




int main(){
int ji,aa,bb;double hui,shui;
while(cin>>n>>m>>ji>>num)
{
V=1;
for(int i=1;i<=m;i++){
cin>>aa>>bb>>hui>>shui;
add_edge(aa,bb,hui,shui);
cin>>hui>>shui;
add_edge(bb,aa,hui,shui);
}
bool flag=bellman_floyd(ji);
if(!flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
memset(cun,0,sizeof(cun));
}
return 0;}</span>
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/lwgkzl/article/details/53100333
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞