poj - 1860 Currency Exchange Bellman-Ford 判断正环

Currency Exchange POJ - 1860 

题意:

  有许多货币兑换点,每个兑换点仅支持两种货币的兑换,兑换有相应的汇率和手续费。你有s这个货币 V 个,问是否能通过合理地兑换货币,使得你手中的货币折合成s后是有增加的。

思路:

  这道题在建立每种货币的兑换关系后,找到图中的正环即可,因为你沿着正环跑就可以增加价值。这里可以用类似Bellman_Ford判断负环的方法。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue



typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n'

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;       
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);



template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x=f?-x:x;
}


/*-----------------------showtime----------------------*/
                const int maxn = 1200;
                struct edge
                {
                        int u,v;
                        double c,r; 
                }e[maxn];
                int n,m,s,tot = 0;
                bool flag;
                double val,dis[maxn];
                void add(int u,int v,double c,double r){
                    e[++tot].u = u;
                    e[tot].v = v;
                    e[tot].c = c;
                    e[tot].r = r;
                }
                double get(int i){
                    return 1.0*(dis[e[i].u] - e[i].c) * e[i].r;
                }
                bool bellman_ford(){
                    for(int i=1; i<=n; i++)dis[i] = 0.0;
                    dis[s] = val;

                    for(int i=1; i<n; i++){
                        flag = true;
                        for(int j=1; j<=tot; j++){
                            if(dis[e[j].v] < get(j))
                            {
                                dis[e[j].v] = get(j);
                                flag = false;
                            }
                        }
                        if(dis[s] > val)return true;
                        if(flag) break;
                    }
                    for(int i=1; i<=tot; i++){
                        if(dis[e[i].v] < get(i)){
                            return true;
                        }
                    }
                    return false;
                }
int main(){
                scanf("%d%d%d%lf", &n, &m, &s, &val);
                int u,v;double c1,r1,c2,r2;
                for(int i=1; i<=m; i++){
                   
                    scanf("%d%d%lf%lf%lf%lf",&u,&v,&r1,&c1,&r2,&c2);
                    add(u,v,c1,r1);
                    add(v,u,c2,r2);
                }
                if(bellman_ford())puts("YES");
                else puts("NO");
                return 0;
}
POJ - 1860

猜你喜欢

转载自www.cnblogs.com/ckxkexing/p/9582342.html