# Currency Exchange（最短路 + Bellman Ford）

Currency Exchange

 Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17890 Accepted: 6314

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`

题意：

给出N（1 ~ 100），M（1 ~ 100），S（1 ~ N），V（0 ~ 1000），分别代表有 N 种钱币，M 条钱币关系，当前所拥有的钱币种类 S，当前拥有钱币种类 S 的总值。后给出 M 跳关系，每条关系共有六个数，首先给出 A，B，代表这是A和B直接的关系。后有四个数 Rab，Cab，Rba，Cba，分别代表前者兑换后者的比率和税制，比如A要换成B的话，则 B = （A – Cab） * Rab。问是否有方法可以使最初拥有的S钱币升值，即使拥有S的钱币大于V。

思路：

最短路。Bellman Ford 判断是否有正环，时间条件满足迭代的写法。

理解题目理解很久，Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence ，因为这句话以为每个节点最多只能用 1 次，于是死活分析不出样例，后来才知道可以重复运用多次后使之升值。后来理解题目之后还是没有想到用Bellman Ford，竟然把 Dijkstra 当成 SPFA 来用虽然AC了，但是代码略丑，补上 Bellman Ford 简洁的迭代做法。

AC：

Once：

``````#include <cstdio>
#include <queue>
#include <utility>
#include <iostream>
#include <vector>
#include <string.h>
#define MAX 300
using namespace std;

typedef pair<double,int> pii;
typedef struct {
double r,c;
}edge;

edge w[MAX];
double d[105],mon;
int fir[105],v[MAX],next[MAX],vis[105],time[105];
int n,m,s,ind;

double calculate(double mon,edge a) {
return (mon - a.c) * a.r;
}

void add_edge(int f,int t,double r,double c) {
v[ind] = t;
w[ind].r = r;
w[ind].c = c;
next[ind] = fir[f];
fir[f] = ind;
ind++;
}

int Dijkstra() {
memset(d,0,sizeof(d));
memset(vis,0,sizeof(vis));
memset(time,0,sizeof(time));
d[s] = mon;
priority_queue<pii,vector<pii>,greater<pii> > q;
q.push(make_pair(d[s],s));
time[s]++;
while(!q.empty()) {
pii k = q.top();q.pop();
int x = k.second;
vis[x] = 0;
for(int e = fir[x];e != -1;e = next[e]) {
int y = v[e];
double res = calculate(d[x],w[e]);
if(d[y] < res) {
d[y] = res;
if(!vis[y]) {
q.push(make_pair(d[y],y));
time[y]++;
vis[y] = 1;
if(time[y] > n) return 1;
}
}
}
}

return 0;
}

int main() {
scanf("%d%d%d%lf",&n,&m,&s,&mon);
ind = 0;
memset(fir,-1,sizeof(fir));
while(m--) {
int a,b;
double r,c;
scanf("%d%d%lf%lf",&a,&b,&r,&c);
scanf("%lf%lf",&r,&c);
}

if(Dijkstra())  puts("YES");
else            puts("NO");

return 0;
}
``````

Twice：

``````#include <stdio.h>
#include <string.h>
#define MAX 500

typedef struct {
double r,c;
}node;

node w[MAX];
int u[MAX],v[MAX];
double d[205];
int n,ind;

void add_edge(int f,int t,double r,double c) {
u[ind] = f;
v[ind] = t;
w[ind].r = r;
w[ind].c = c;
ind++;
}

double cal(double x,node a) {
return (x - a.c) * a.r;
}

int Bellman_ford(int s,double mon) {
memset(d,0,sizeof(d));
d[s] = mon;
for(int i = 1;i < n;i++)
for(int j = 0;j < ind;j++){
int x = u[j],y = v[j];
double res = cal(d[x],w[j]);
if(d[x] > 0 && d[y] < res)  d[y] = res;
}

for(int j = 0;j < ind;j++) {
int x = u[j],y = v[j];
double res = cal(d[x],w[j]);
if(d[y] < res)  return 1;
}

return 0;
}

int main() {
int m,s;
double mon;
scanf("%d%d%d%lf",&n,&m,&s,&mon);
ind = 0;
while(m--) {
int a,b;
double r,c;
scanf("%d%d%lf%lf",&a,&b,&r,&c);
scanf("%lf%lf",&r,&c);
}

if(Bellman_ford(s,mon))  puts("YES");
else    puts("NO");
return 0;
}
``````

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