poj1860(Bellman-Ford)

我们的城市有几个货币兑换点。让我们假设每一个点都只能兑换专门的两种货币。可以有几个点,专门从事相同货币兑换。每个点都有自己的汇率,外汇汇率的A到B是B的数量你1A。同时各交换点有一些佣金,你要为你的交换操作的总和。在来源货币中总是收取佣金。 例如,如果你想换100美元到俄罗斯卢布兑换点,那里的汇率是29.75,而佣金是0.39,你会得到(100 - 0.39)×29.75=2963.3975卢布。 你肯定知道在我们的城市里你可以处理不同的货币。让每一种货币都用唯一的一个小于N的整数表示。然后每个交换点,可以用6个整数表描述:整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金。 nick有一些钱在货币S,他希望能通过一些操作(在不同的兑换点兑换),增加他的资本。当然,他想在最后手中的钱仍然是S。帮他解答这个难题,看他能不能完成这个愿望。 

Input

第一行四个数,N,表示货币的总数;M,兑换点的数目;S,nick手上的钱的类型;V,nick手上的钱的数目;1<=S<=N<=100, 1<=M<=100, V 是一个实数 0<=V<=103. 接下来M行,每行六个数,整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金(0<=佣金<=102,10-2<=汇率<=102)  4

Output

如果nick能够实现他的愿望,则输出YES,否则输出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


//题意简单,判断正环,原点值增加即为真
#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;
                dis[x2] = (dis[x1] - hy[j][1]) * hy[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;
            tol++;
            b[tol][0] = a2, b[tol][1] = a1;
            hy[tol][0] = r2, hy[tol][1] = c2;
            tol++;
        }
        if(bellman(s, n, v)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/zxybdnb/p/11939410.html