• poj1860 Currency Exchange(spfa判断正环)


    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个兑换点,每个兑换点可以在收取Cab(币种为A)佣金(佣金固定!)后将A按Rab汇率换成B,也可在收取Cba(币种为B)佣金后将B按Rab汇率换成A,你现在有币种为S(我就不说S币了)的货币V元
    请问你是否可以通过某些兑换将S换成其他货币,再换回S以获得更多的钱?
    题解:很明显的一道spfa求正环,bfs方法中只需要一个点进队超过n次即构成正环
    求正环吗,自然求的是最长路,而且d[x]+w>y也要改成d[x]*w>y,这也是常识了
    代码如下:
    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    struct node
    {
        int x;
        double r,c;
    };
    
    vector<node> g[110];
    double d[110],c;
    int vis[110],cnt[110];
    int n,m,s;
    
    int check(int u)
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            d[i]=0;
        }
        d[u]=c;
        queue<int> q;
        q.push(u);
        while(!q.empty())
        {
            int x=q.front();
            int sz=g[x].size();
            vis[x]=0;
            q.pop();
            for(int i=0;i<sz;i++)
            {
                int y=g[x][i].x;
                double r=g[x][i].r,c=g[x][i].c;
                if((d[x]-c)*r>d[y])
                {
                    d[y]=(d[x]-c)*r;
                    cnt[y]++;
                    if(!vis[y])
                    {
                        q.push(y);
                        vis[y]=1;
                        if(cnt[y]==n)
                        {
                            return 1;
                        }
                    }
                }
            }
        }
        return 0;
    }
    
    int main()
    {
        scanf("%d%d%d%lf",&n,&m,&s,&c);
        for(int i=1;i<=m;i++)
        {
            int f,t;
            double rab,cab,rba,cba,c1,c2;
            scanf("%d%d%lf%lf%lf%lf",&f,&t,&rab,&cab,&rba,&cba);
            node hehe;
            hehe.x=t;
            hehe.r=rab;
            hehe.c=cab;
            g[f].push_back(hehe);
            hehe.x=f;
            hehe.r=rba;
            hehe.c=cba;
            g[t].push_back(hehe);
        } 
        int ans=check(s);
        if(ans)
        {
            printf("YES
    ");
        }
        else
        {
            printf("NO
    ");
        }
    }


  • 相关阅读:
    Android Wear真机蓝牙调试方法及错误解决方法,设备华为WATCH+小米5
    Ubuntu16.04 Selenium+python 环境搭建 Chromedriver安装
    Ubuntu16.04 为vim安装YouCompleteMe插件
    codeforces 676C Vasya and String 贪心 尺取法
    poj 1177 & hdu 1828 Picture 线段树 扫描线求矩形周长并
    hdu 1542 & poj 1151 Atlantis 线段树扫描线求矩形面积并
    fzu 2109 Mountain Number 数位DP
    fzu 2105 Digits Count 线段树
    codeforces 675D Tree Construction 数据结构
    codeforces 675C Money Transfers 贪心
  • 原文地址:https://www.cnblogs.com/stxy-ferryman/p/8438350.html
Copyright © 2020-2023  润新知