zoukankan      html  css  js  c++  java
  • POJ1860(ford判环)

    Currency Exchange
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 24243   Accepted: 8813

    Description

    Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency. 
    For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR. 
    You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real RAB, CAB, RBA and CBA - exchange rates and commissions when exchanging A to B and B to A respectively. 
    Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations. 

    Input

    The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=103
    For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10-2<=rate<=102, 0<=commission<=102
    Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 104

    Output

    If Nick can increase his wealth, output YES, in other case output NO to the output file.

    Sample Input

    3 2 1 20.0
    1 2 1.00 1.00 1.00 1.00
    2 3 1.10 1.00 1.10 1.00
    

    Sample Output

    YES
    题意:有N种货币,M种交换点。将货币a换为货币b时所换到的 货币b价值=(货币a价值-手续费c)*利率r。问给定一种货币S,其价值为V,问是否存在交换方式使货币S交换一圈回来之后其价值变大。
    思路:将货币视作结点,交换过程视为路径,利用ford算法,判断图中是否存在无限迭代的环。
    /*
        1860    Accepted    404K    16MS
    */
    #include"cstdio"
    #include"cstring"
    using namespace std;
    const int MAXN=1005;
    struct Edge{
        int from,to;
        double r,c;
    }es[MAXN];
    int n,E;
    bool ford(int s,double v)
    {
        double d[MAXN];
        memset(d,0,sizeof(d));
        d[s]=v;
        while(n--)
        {
            bool update=false;
            for(int i=0;i<E;i++)
            {
                Edge e=es[i];
                if(d[e.from]!=0&&d[e.to]<(d[e.from]-e.c)*e.r)
                {
                    d[e.to]=(d[e.from]-e.c)*e.r;
                    update=true;    
                }
            }
            if(!update)    break;
        }
        //由ford算法可得:若不存在负环,经过n-1迭代,必能迭代完毕 
        if(n==-1)    return true;
        return false;
    }
    
    int main()
    {
        int N,M,S;
        double V;
        while(scanf("%d%d%d%lf",&N,&M,&S,&V)!=EOF)
        {
            E=0;
            n=N;
            for(int i=0;i<M;i++)
            {
                int a,b;
                double rab,cab,rba,cba;
                scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
                es[E].from=a,es[E].to=b,es[E].r=rab,es[E++].c=cab;
                es[E].from=b,es[E].to=a,es[E].r=rba,es[E++].c=cba;
            }
            
            if(ford(S,V))    printf("YES
    ");
            else    printf("NO
    ");        
        }
        
        return 0;
    }

     若存在越滚越大的环则财富可以增长。

    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int MAXN=205;
    struct Edge{
        int to,net;
        double r,c;
    }es[MAXN];
    struct Node{
        int nod;
        double captial;
        Node(){}
        Node(int nod,double captial)
        {
            this->nod=nod;
            this->captial=captial;
        }
    };
    int head[MAXN],tot;
    int n,m,src;
    double wealth;
    double d[MAXN];
    int cnt[MAXN];
    void addedge(int u,int v,double r,double c)
    {
        es[tot].to=v;
        es[tot].r=r;
        es[tot].c=c;
        es[tot].net=head[u];
        head[u]=tot++;
    }
    bool spfa()
    {
        memset(cnt,0,sizeof(cnt));
        memset(d,0,sizeof(d));
        d[src]=wealth;
        queue<Node> que;
        que.push(Node(src,wealth));
        while(!que.empty())
        {
            Node now=que.front();que.pop();
            for(int i=head[now.nod];i!=-1;i=es[i].net)
            {
                double money=(now.captial-es[i].c)*es[i].r;
                if(money>d[es[i].to])
                {
                    d[es[i].to]=money;
                    cnt[es[i].to]++;
                    if(cnt[es[i].to]==n)    return true;
                    que.push(Node(es[i].to,money));
                }
            }
        }
        return false;
    }
    int main()
    {
        while(scanf("%d%d%d%lf",&n,&m,&src,&wealth)!=EOF)
        {
            memset(head,-1,sizeof(head));
            tot=0;
            for(int i=0;i<m;i++)
            {
                int u,v;
                double r1,c1,r2,c2;
                scanf("%d%d%lf%lf%lf%lf",&u,&v,&r1,&c1,&r2,&c2);
                addedge(u,v,r1,c1);
                addedge(v,u,r2,c2);
            }
            if(spfa())
            {
                printf("YES
    ");
            }
            else
            {
                printf("NO
    ");
            }
        }
        return 0;
    }
  • 相关阅读:
    在浏览器中浏览git上项目目录结构
    部署elasticsearch(三节点)集群+filebeat+kibana
    谷歌浏览器安装Elasticsearch-head 插件
    Logstash配置文件修改自动加载和指定目录进行启动
    使用Dbvisualizer 连接 Elasticsearch
    Elasticsearch常见用法-分布式集群
    Elasticsearch常见用法-入门
    Elastic Stack 7.5.0白金版永不过期
    配置 Nginx 反向代理 WebSocket
    ES7.3.0配置
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5149779.html
Copyright © 2011-2022 走看看