# POJ 1860:Currency Exchange(Bellman_Ford算法）

Currency Exchange

 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12217 Accepted: 4120

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

```
Bellman-Ford算法描述：
1,.初始化：将除源点外的所有顶点的最短距离估计值 d[v] ←+∞, d[s] ←0;
2.迭代求解：反复对边集E中的每条边进行松弛操作，使得顶点集V中的每个顶点v的最短距离估计值逐步逼近其最短距离；（运行|v|-1次）
3.检验负权回路：判断边集E中的每一条边的两个端点是否收敛。如果存在未收敛的顶点，则算法返回false，表明问题无解；否则算法返回true，并且从源点可达的顶点v的最短距离保存在 d[v]中。```

首先指出，图的任意一条最短路径既不能包含负权回路，也不会包含正权回路，因此它最多包含|v|-1条边。
其次，从源点s可达的所有顶点如果 存在最短路径，则这些最短路径构成一个以s为根的最短路径树。Bellman-Ford算法的迭代松弛操作，实际上就是按顶点距离s的层次，逐层生成这棵最短路径树的过程。

``````#include<iostream>
using namespace std;

#define eps 1e-9
#define MAX_N  105

struct edge
{
int u;
int v;
double rate;
double cost;
}edge[2*MAX_N];

int N,M,s,acrNum;
double V;

bool Bellman_Ford()
{
bool flag;	//判断能否收敛
int i;
double d[MAX_N];
for(i=1;i<=N;i++)
d[i] = 0.0;
d[s] = V;
while(d[s] <= V+eps)
{
flag = true;
for(i=0;i<acrNum;i++)
{
double tmp = (d[edge[i].u] - edge[i].cost)*edge[i].rate;
if(d[edge[i].v] + eps < tmp)
{
flag = false;
d[edge[i].v] = tmp;
}
}
if(flag)	//没有收敛，则是无法找到
return (d[s]-V) > 0;
}
return 1;
}

int main()
{
while(scanf("%d%d%d%lf",&N,&M,&s,&V)!=EOF)
{
int A,B;
double Rab,Cab,Rba,Cba;
for(int i=0;i<M;i++)
{
scanf("%d%d%lf%lf%lf%lf",&A,&B,&Rab,&Cab,&Rba,&Cba);
//A->B
edge[i].u = A;		edge[i].v = B;
edge[i].rate = Rab;	edge[i].cost = Cab;
//B->A
edge[i+M].u = B;	edge[i+M].v = A;
edge[i+M].rate = Rba;	edge[i+M].cost = Cba;
}
acrNum = 2*M;
if(Bellman_Ford())	printf("YES\n");
else				printf("NO\n");
}
return 0;
}``````

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