POJ1860 SPFA

2017年3月25日 | ljfcnyali
题目大意
给定N种货币,某些货币之间可以相互兑换,现在给定一些兑换规则,问能否从某一种货币开始兑换,经过一些中间货币之后,最后兑换回这种货币,并且得到的钱比之前的多。

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

1
YES
题目分析
使用SPFA判断负权环,一个一个乱搞就可以了。
AC代码

/*************************************************************************
    > File Name: POJ1860.cpp
    > Author: ljf-cnyali
    > Mail: ljfcnyali@gmail.com 
    > Created Time: 2017/3/25 15:00:46
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>

using namespace std;

#define REP(i, a, b) for(int i = (a), _end_ = (b);i <= _end_; ++ i)
#define mem(a) memset((a), 0, sizeof(a))
#define str(a) strlen(a)

const int maxn = 210;

int n, m, S;

int begin[maxn], next[maxn], to[maxn], e, vis[maxn], Num[maxn];

double V, r[maxn], c[maxn], dist[maxn];

void add(int x, int y, double a, double b) {
    to[++ e] = y;
    next[e] = begin[x];
    begin[x] = e;
    r[e] = a;
    c[e] = b;
}

bool SPFA() {
    queue<int> Q;
    vis[S] = 1;
    dist[S] = V;
    Q.push(S);
    Num[S] ++;
    while(!Q.empty()) {
        int p = Q.front();
        Q.pop();
        vis[p] = 0;
        for(int i = begin[p]; i; i = next[i]) {
            int q = to[i];
            if(dist[q] < (dist[p] - c[i]) * r[i]) {
                dist[q] = (dist[p] - c[i]) * r[i];
                if(!vis[q]) {
                    vis[q] = 1;
                    Q.push(q);
                    Num[q] ++;
                    if(Num[q] > n)
                        return true;
                }
            }
        }
    }
    if(dist[S] > V)
        return true;
    return false;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int x, y;
    double r1, c1, r2, c2;
    while(scanf("%d%d%d%lf", &n, &m, &S, &V) != EOF) {
        e = 0;
        mem(begin);mem(next);mem(to);mem(vis);mem(r);mem(c);
        REP(i, 1, m) {
            scanf("%d%d%lf%lf%lf%lf", &x, &y, &r1, &c1, &r2, &c2);
            add(x, y, r1, c1);
            add(y, x, r2, c2);
        }
        if(SPFA())
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}

本文转自:http://ljf-cnyali.cn/index.php/archives/111

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值