zoukankan      html  css  js  c++  java
  • POJ 1860 (SPFA判断正环)

      题意:我们的城市有几个货币兑换点。让我们假设每一个点都只能兑换专门的两种货币。可以有几个点,专门从事相同货币兑换。每个点都有自己的汇率,外汇汇率的A到B是B的数量你1A。同时各交换点有一些佣金,你要为你的交换操作的总和。在来源货币中总是收取佣金。

    例如,如果你想换100美元到俄罗斯卢布兑换点,那里的汇率是29.75,而佣金是0.39,你会得到(100 - 0.39)×29.75=2963.3975卢布。

    你肯定知道在我们的城市里你可以处理不同的货币。让每一种货币都用唯一的一个小于N的整数表示。然后每个交换点,可以用6个整数表描述:整数a和b表示两种货币,a到b的汇率,a到b的佣金,b到a的汇率,b到a的佣金。

      问:nick有一些钱在货币S,他希望能通过一些操作(在不同的兑换点兑换),增加他的资本。当然,他想在最后手中的钱仍然是S。帮他解答这个难题,看他能不能完成这个愿望。

      思路:这题翻译好坑。就是裸SPFA判正环,注意存在double类型,有些细节要处理

    #include <iostream>
    #include <cmath>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <map>
    #include <iomanip>
    #include <algorithm>
    #include <queue>
    #include <stack>
    #include <set>
    #include <vector>
    //const int maxn = 1e5+5;
    #define ll long long
    ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
    //const int inf = 0x6fffffff;
    
    #define MAX INT_MAX
    #define FOR(i,a,b) for( int i = a;i <= b;++i)
    #define bug cout<<"--------------"<<endl
    using namespace std;
    int ver[210],head[210],next[210],vis[210],ans[210];
    int tot,n,m,first;
    double money;
    double hl[210],lx[210],d[210];
    void add(int x,int y,double h,double l)
    {
        ver[++tot] = y,hl[tot] = h,lx[tot] = l,next[tot] = head[x] ,head[x] = tot;
    }
    int spfa()
    {
        queue<int>que;
        memset(vis,0,sizeof(vis));
        memset(d,0,sizeof(d));
        d[first] = money;
        vis[first] = 1;
        ans[first] = 1;
        que.push(first);
    
        while(que.size())
        {
            int x = que.front(); que.pop();
            vis[x] = 0;
    
            for(int i=head[x];i;i=next[i])
            {
                int y = ver[i];
                double h = hl[i], l = lx[i];        //cout<<x<<" "<<y<<endl;
                if( d[y] < ( d[x] - l) * h)
                {
                    d[y] = (d[x] - l) * h;
                    if(vis[y] == 0)
                    {
                        vis[y] =1;
                        que.push(y);
                        ans[y]++;
                        if(ans[y] >= n)
                        {
                            return 1;
                        }
                    }
                }
            }
        }
        return 0;
    
    }
    int main()
    {
          ios::sync_with_stdio(false);
        cin>>n>>m>>first>>money;
        while(m--)
        {
            int a,b;
            double ab1,ab2,ba1,ba2;
            cin>>a>>b>>ab1>>ab2>>ba1>>ba2;
            add(a,b,ab1,ab2);
            add(b,a,ba1,ba2);
        }
        if(spfa() == 1) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
  • 相关阅读:
    文字
    <script type="text/x-template"> 模板
    防xss攻击
    url
    symmfony
    composer
    header 和http状态码
    bootstrap
    linux的设置ip连接crt,修改主机名,映射,建文件
    Centos上传下载小工具lrzsz
  • 原文地址:https://www.cnblogs.com/jrfr/p/11364261.html
Copyright © 2011-2022 走看看