POJ 1860 Currency Exchange(求环变形)

本文介绍了一个使用Bellman-Ford算法判断是否存在负权回路的问题解决方法。通过具体的代码实现,展示了如何初始化图结构、添加边,并运行Bellman-Ford算法来检测负环的存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:
。。。
思路:
Bellman-ford判环

//#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <set>
#include <climits>
#include <map>
#include <iomanip>

using namespace std;

#define SPEED_UP iostream::sync_with_stdio(false);
#define FIXED_FLOAT cout.setf(ios::fixed, ios::floatfield);
#define rep(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define urep(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
#define in_bound(l, r, i) (l)<=(i)&&(i)<(r)
#define pb push_back

typedef long long LL;

const int inf = INT_MAX/2;
const int Maxn = 100;

struct Edge{
    int from, to;
    double r, c;
    Edge() {}
    Edge(int x, int y, double z, double z2):from(x), to(y), r(z), c(z2){}
};

int n, m, beg;
double d[Maxn+5], S;
vector<int> g[Maxn+5];
Edge E[1000+5];
map<string, int> id;

int go () {
    rep(i, 1, n) d[i] = 0;
    d[beg] = S;
    rep (k, 1, n) {
        rep(i, 1, 2*m) {
            Edge &e = E[i];
            if ( (d[e.from]-e.c) * e.r > d[e.to]) {
                //cout << "to " << e.to << " : " <<  (d[e.from]-e.c) * e.r << endl;
                if (k == n) return 1;
                d[e.to] = (d[e.from]-e.c) * e.r;
            }
        }
    }
    return 0;
}

int solve() {
    return go();
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
#endif
    SPEED_UP

    cin >> n >> m >> beg >> S;

    int A, B;
    double rAB, cAB, rBA, cBA;
    rep(i, 1, m) {
        cin >> A >> B >> rAB >> cAB >> rBA >> cBA;
        E[i] = Edge(A, B, rAB, cAB);
        E[i+m] = Edge(B, A, rBA, cBA);
    }
    if (solve()) cout << "YES\n";
    else cout << "NO\n";
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值