poj 1860 Currency Exchange(Bellman-Ford 改)

poj 1860 Currency Exchange

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.

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.

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


题目大意:第一行数据n, m, S, V,分别代表:货币种数,货币间交换情况,Nick拥有的货币种类,Nick拥有的货币数量。接下来m行,每行6个数据,分别代表着:货币a, 货币b, 货币a到货币b的汇率,货币a到货币b的手续费,货币b到货币a的汇率, 货币b到货币a的手续费。给出这些信息,问经过交换,S类的货币能否得到增长。 b=aab×ab


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

const int N = 300;
const int INF = 0x3f3f3f3f;
typedef long long ll;
int n, m, S, pos;
double V;

struct Cur{
    int a, b;
    double rate, com;

void addEdge(int u, int v, double r, double c) {
    C[pos].a = u;
    C[pos].b = v;   
    C[pos].rate = r;
    C[pos].com = c;

void input() {
    pos = 0;
    int a, b;
    double ra, rb, ca, cb;
    for (int i = 0; i < m; i++) {
        scanf("%d %d %lf %lf %lf %lf", &a, &b, &ra, &ca, &rb, &cb); 
        addEdge(a, b, ra, ca);
        addEdge(b, a, rb, cb);

double d[N];
int BF() {      //Bellman-Ford判正环
    int u, v;
    for (int i = 0; i <= n; i++) d[i] = 0;
    d[S] = V;
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < pos; j++) {
            u = C[j].a;
            v = C[j].b;
            if (d[u] > 0) {
                d[v] = max(d[v], (d[u] - C[j].com) * C[j].rate);    
    for (int i = 0; i < pos; i++) {
        u = C[i].a; 
        v = C[i].b;
        if (d[v] < (d[u] - C[i].com) * C[i].rate) {
            return 1;
    return 0;

int main() {
    while (scanf("%d %d %d %lf", &n, &m, &S, &V) == 4) {
        if (BF()) printf("YES\n");
        else printf("NO\n");
    return 0;
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/llx523113241/article/details/47065565