POJ 1860(判断图中有无正环)

题意:
给你n中货币,以及m个货币转换的关系,若a的钱为money, a转b的汇率为rate,手续费为cost,那a转b的钱就是(money-cost)*rate,现在给你一定数量的种类为s的货币,现在问你能不能进行货币转换,最后还是变成s的货币,让这个数量比一开始的多。

思路:
找有无正环,代码写法其实就是最长路+找负环(判断一个点是不是松弛超过n次)。

/**
* Author : zzy
* Date : 2020-04-20-21.23.08 Monday
*/
#include <stdio.h>
#include <vector>
#include <algorithm>
#include <string.h>
#include <limits.h>
#include <string>
#include <iostream>
#include <queue>
#include <math.h>
#include <map>
#include <stack>
#include <sstream>
#include <set>
#include <iterator>
#include <list>
#include <cstdio>
#include <iomanip>

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) ((int)(x).size())

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

const int maxn = 2600;
const i64 INF = 0x3f3f3f3f3f3f3f3fLL;
struct node {
    int id;
    double change, cost;
    double now;
    node() {}
    node(int a, double b, double c) : id(a), change(b), cost(c) {}
    node(int a) : id(a) {}
};
vector<node> G[maxn];
double dis[maxn], val;
int in[maxn];
bool vis[maxn];

bool spfa(int s, int n) {
    queue<node> q;
    node cur;
    ms(vis, 0);
    for (int i = 1; i <= n; ++i) dis[i] = -INF;
    vis[s] = 1;
    dis[s] = val;
    q.push(node(s));
    while (!q.empty()) {
        cur = q.front();
        q.pop();
        vis[cur.id] = 0;
        vector<node>::iterator it = G[cur.id].begin();
        for (it; it != G[cur.id].end(); ++it) {
            node to = *it;
            if (dis[to.id] < (dis[cur.id]-to.cost)*to.change) {
                //判断负(正)环在这++ , 如果>=n就return true
                ++in[to.id];
                if (in[to.id] >= n) return true;
                dis[to.id] = (dis[cur.id]-to.cost)*to.change;
                if (!vis[to.id]) {
                    q.push(node(to.id));
                    vis[to.id] = 1;
                }
            }
        }
    }
    return false;
}
void init(int n) {
    for (int i = 0; i <= n; ++i) G[i].clear();
    ms(in, 0);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif


     int n, m, s, t;
    cin >> n >> m >> s >> val;
    init(n);
    forn(i, m) {
        int u, v;
        double r1, c1, r2, c2;
        cin >> u >> v >> r1 >> c1 >> r2 >> c2;
        G[u].pb(node(v, r1, c1));
        G[v].pb(node(u, r2, c2));
    }
    bool flag = spfa(s, n); //s是起点,n是点数
    cout << (flag?"YES":"NO") << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值