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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章