poj1860(Bellman-Ford)

Our city has several points of currency exchange. Let us assume that each point can redeem a special two currencies. There may be a few points, which specializes in the same currency exchange. Each point has its own currency, the exchange rate of A to B and B is the number of your 1A. At the same time there are some points of each exchange commission, the sum you want to exchange your operation. Money is always the source of commission. For example, if you want to change $ 100 to Russian Ruble exchange points, where the exchange rate is 29.75, while the commission is 0.39, you will get (100 - 0.39) × 29.75 = 2963.3975 rubles. You must know that in our city you can handle different currencies. Let each currency are represented by only one integer less than N. Then each switching point, integer table 6 can be described: a and b denote an integer of two currencies, exchange a to b, b to a commission, to a currency b, b to a commission. nick have some money in the currency S, he wants to pass some operations (exchange at different exchange points), to increase his capital. Of course, he wanted the money in the hands of the last still is S. To help him solve this difficult problem, see if he can accomplish this desire. 

Input

The first four row number, N, denotes the total number of currencies; number M, the exchange points; Type S, nick hand money; number V, nick hand money; 1 <= S <= N <= 100, 1 <= M <= 100, V is a real number 0 <= V <= 103. Next M rows, each row of six numbers, integers a and b represent two currencies, exchange of b to a, to a commission b, b to exchange a, b to a commission (0 <= commission <= 102,10-2 <= Rates <= 102)  4

Output

If nick able to realize his wish, output YES, otherwise output NO.

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 


// simple meaning of the questions, true or rings, increase value is the true origin
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <stdio.h>
#include <cmath>
#include <string.h>
#include <vector>

#define ll long long
using namespace std;

int b[210][2], tol;
double hy[210][2], dis[110];

bool bellman(int s, int n, double v)
{
    for(int i = 1; i <= n; i++) dis[i] = 0;
    dis[s] = v;
    for(int i = 1; i <= n; i++)
    {
        bool flag = 0;
        for(int j = 0; j < tol; j++)
        {
            int x1 = b[j][0], x2 = b[j][1];
            if(dis[x2] < (dis[x1] - hy[j][1]) * hy[j][0])
            {
                flag = 1;
                it [x2] = (it [x1] - he [j] [ 1 ]) * he [j] [ 0 ];
            }
        }
        if(flag == 0) return 0;
    }
    for(int i = 0; i < tol; i++)
        if(dis[b[i][1]] < (dis[b[i][0]] - hy[i][1]) * hy[i][0])
            return 1;
    return 0;
}

int main ()
{
    ios::sync_with_stdio(false);
    double v;
    int n, m, s;
    while(~scanf("%d%d%d%lf", &n, &m, &s, &v))
    {
        tol = 0;
        while(m--)
        {
            int a1, a2;
            double r1, c1, r2, c2;
            scanf("%d%d%lf%lf%lf%lf", &a1, &a2, &r1, &c1, &r2, &c2);
            b[tol][0] = a1, b[tol][1] = a2;
            Hy [Tol] [ 0 ] = R1, Hy [Tol] [ 1 ] = c1 , ;
            toll ++ ;
            b[tol][0] = a2, b[tol][1] = a1;
            Hy [Tol] [ 0 ] = R2, Hy [Tol] [ 1 ] = , c2 , ;
            toll ++ ;
        }
        if(bellman(s, n, v)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zxybdnb/p/11939410.html