poj1860 最短路 无限增大环

来源:互联网 发布:手机mac地址修改器 编辑:程序博客网 时间:2024/06/07 02:37
Currency Exchange
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 22980 Accepted: 8294

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.01 2 1.00 1.00 1.00 1.002 3 1.10 1.00 1.10 1.00

Sample Output

YES

Source

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#define INF 10000000.0using namespace std;struct node{    int u,v;    double r;    double c;} edge[10000];int n,m,s;double v;double low[10000];int a,b;double rab,cab,rba,cba;int num=0;int Bellman(int u0){    for(int i=0; i<=n; i++)        low[i]=0;    low[u0]=v;    for(int i=0; i<n-1; i++)    {        int flag=0;        for(int j=0; j<num; j++)        {            if((low[edge[j].u]-edge[j].c)*edge[j].r>low[edge[j].v]+(1e-10))            {                low[edge[j].v]=(low[edge[j].u]-edge[j].c)*edge[j].r;                flag=1;            }        }        if (flag==0)            break;    }    for(int j=0; j<num; j++)//判断有无无限增加环   ,类似判负权回路    {        if((low[edge[j].u]-edge[j].c)*edge[j].r>low[edge[j].v]+(1e-10))            return 1;    }    return 0;}int main(){    while(~scanf("%d%d%d%lf",&n,&m,&s,&v))//定义了double不可用%f读入    {        num=0;        for(int i=0; i<m; i++)        {            scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);            edge[num].u=a;            edge[num].v=b;            edge[num].r=rab;            edge[num++].c=cab;            edge[num].u=b;            edge[num].v=a;            edge[num].r=rba;            edge[num++].c=cba;        }        if(Bellman(s))            printf("YES\n");        else            printf("NO\n");    }}
  /*int Bellman(int u0){for(int i=0;i<=n;i++)low[i]=0;low[u0]=v;while(low[u0]<=v+(1e-10))     //此处的兑换可以无限进行,所以就不是递推 n 了;{                                                          <span id="transmark"></span>//可以存在可以无限增大的环int flag=0;                               for(int j=0;j<num;j++){if((low[edge[j].u]-edge[j].c)*edge[j].r>low[edge[j].v]+(1e-10)){low[edge[j].v]=(low[edge[j].u]-edge[j].c)*edge[j].r;flag=1;}}if (flag==0)break;}if(low[u0]>v)return 1;return 0;}*/
spfa版
<pre name="code" class="cpp">#include<algorithm>#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<queue>#define LL long long#define INF 0x3f3f3f3f#define eps 0.000000001#define N 100000#define bug puts("***************");using namespace std;struct node{    int to;    double r,c;}p1;vector<node>vec[1010];queue<int>Q;int n,m,s;double v;int vis[10000];double bene[10000];int num[10000];void spfa(){    memset(vis,0,sizeof(vis));    memset(bene,0,sizeof(bene));    memset(num,0,sizeof(num));    while(!Q.empty())Q.pop();    Q.push(s);    bene[s]=v;    num[s]++;    vis[s]=1;    while(!Q.empty()){        int u=Q.front();        Q.pop();        vis[u]=0;        //cout<<num[u]<<endl;        if(num[u]>n){            printf("YES\n");            return ;        }        int len=vec[u].size();        for(int i=0;i<len;i++){            node p=vec[u][i];            if(bene[p.to]<(bene[u]-vec[u][i].c)*vec[u][i].r){               bene[p.to]=(bene[u]-vec[u][i].c)*vec[u][i].r;                if(!vis[p.to]){                num[p.to]++;                vis[p.to]=1;                Q.push(p.to);               }            }        }    }    printf("NO\n");    return ;}int main(){    while(~scanf("%d%d%d%lf",&n,&m,&s,&v)){        int u,v;        double rab,cab,rba,cba;        for(int i=0;i<m;i++){            scanf("%d%d%lf%lf%lf%lf",&u,&v,&rab,&cab,&rba,&cba);            p1.to=v,p1.r=rab,p1.c=cab;            vec[u].push_back(p1);            p1.to=u,p1.r=rba,p1.c=cba;            vec[v].push_back(p1);        }       spfa();    }    return 0;}




1 0
原创粉丝点击