# poj Currency Exchange（Bellman_ford变形）

Currency Exchange

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`

``````#include<stdio.h>
#include<string.h>
#define maxn 200+10

struct node
{
int u,v;
double r,c;
} E[maxn];
int n,m,s,len;//n表示钱币种类数，m表示换币的地点数目，s表示当前的钱币种类
double v;//v表示当前的钱币数目
double d[maxn];

void bellman_ford(int st)
{
memset(d,0,sizeof(d));//初始化为最小值
d[st]=v;
for(int i=1; i<=n-1; i++)//迭代n-1次
{
int flag=0;//优化，判断是否存在正权回路，如果存在正权回路，那么必定有值增加
for(int j=1; j<len; j++)//松弛
{
int x=E[j].u,y=E[j].v;
if(d[y]<(d[x]-E[j].c)*E[j].r)
{
d[y]=(d[x]-E[j].c)*E[j].r;
flag=1;
}
}
if(!flag)
{
printf("NO\n");
return ;
}
}
for(int i=1; i<len; i++)//判断是否存在正权回路
{
int x=E[i].u,y=E[i].v;
if(d[y]<(d[x]-E[i].c)*E[i].r)
{
printf("YES\n");
return ;
}
}
printf("NO\n");
}
int main()
{
while(~scanf("%d%d%d%lf",&n,&m,&s,&v))
{
int a,b,i;
double ra,rb,ca,cb;
len=1;
for(i=1; i<=m; i++)
{
scanf("%d%d%lf%lf%lf%lf",&a,&b,&ra,&ca,&rb,&cb);
E[len].u=a,E[len].v=b,E[len].r=ra,E[len].c=ca,len++;
E[len].u=b,E[len].v=a,E[len].r=rb,E[len].c=cb,len++;
}
bellman_ford(s);
}
return 0;
}

``````

ps：另外此题还可用spfa算法解决，只需判断是否有点进入队列的次数大于n次或者在每次松弛后判断是否存在d[s]大于v

原文作者：Bellman - ford算法
原文地址: https://blog.csdn.net/blessLZH0108/article/details/54583833
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。 