POJ 1860 Currency Exchange(求环变形)

来源:互联网 发布:隐马尔可夫算法 编辑:程序博客网 时间:2024/06/05 23:48

题意:
。。。
思路:
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_backtypedef 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;}
0 0
原创粉丝点击