首页 > 其他 > 详细

POJ1860(ford判环)

时间:2016-01-21 23:56:30      阅读:457      评论:0      收藏:0      [点我收藏+]
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\n");
        else    printf("NO\n");        
    }
    
    return 0;
}

 

POJ1860(ford判环)

原文:http://www.cnblogs.com/program-ccc/p/5149779.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!