POJ-1860 Currency Exchange (最短路)

https://vjudge.net/problem/POJ-1860

题意

有多种汇币,汇币之间可以交换,这需要手续费,当你用100A币交换B币时,A到B的汇率是29.75,手续费是0.39,那么你可以得到(100 - 0.39) * 29.75 = 2963.3975 B币。问s币的金额经过交换最终得到的s币金额数能否增加

货币的交换是可以重复多次的,所以我们需要找出是否存在正权回路,且最后得到的s金额是增加的。

分析

建图,一种货币就是一个点,货币交换作为有向边。边的权值需要小心,A到B的权值为(V(A) - C)*R。看到正权回路,应该想到负权回路,那思考一下是不是能用Bellman-Ford来做呢,其实这个问题刚刚好是相反的,这里需要求最长路,那么把dist初始化为0,dist[s]=v,松弛条件相反,利用Bellman-Ford的思想就能解决这道题了。

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>

#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0)

using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
template <class T>
inline bool scan_d(T &ret){
    char c;int sgn;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret = ret*10+(c-'0');
    ret*=sgn;
    return 1;
}
const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = 1000000000;
int T;

void testcase(){
    printf("Case %d:",++T);
}

const int MAXN = 5e5+10 ;
const int MAXM = 150;
const double eps = 1e-8;
const double PI = acos(-1.0);

int m,n,s;
double v;
double dist[105];
int M;
struct exg{
    int a,b;
    double r,c;
}e[205];

void add_edge(int a,int b,double r,double c){
    e[M].a=a;
    e[M].b=b;
    e[M].c=c;
    e[M++].r=r;
}

void init(){
    scanf("%d%d%d%lf",&n,&m,&s,&v);
    M=1;
    int ta,tb;
    double r1,c1,r2,c2;
    for(int i=0;i<m;i++){
        scanf("%d%d%lf%lf%lf%lf",&ta,&tb,&r1,&c1,&r2,&c2);
        add_edge(ta,tb,r1,c1);
        add_edge(tb,ta,r2,c2);
    }
}
void work(){
    return;
}
bool bellman(){
    mset(dist,0);
    dist[s]=v;
    for(int i=1;i<=n;i++){
        for(int j=1;j<M;j++){
            dist[e[j].b]=max(dist[e[j].b],(dist[e[j].a]-e[j].c)*e[j].r);
        }
    }
    for(int i=1;i<M;i++){
        if(dist[e[i].b]<(dist[e[i].a]-e[i].c)*e[i].r) return true;
    }
    return false;
}
int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif // LOCAL
    init();
    if(bellman()) puts("YES");
    else puts("NO");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fht-litost/p/9191409.html