1162. Currency Exchange

http://acm.timus.ru/problem.aspx?space=1&num=1162

被这句话害了 "Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence."

根本没有意义  直径 spfa 就行

代码:

#include<iostream>

#include<stdio.h>

#include<string.h>

#include<math.h>

#include<algorithm>

#include<vector>

#include<set>

#include<map>

#include<string>

#include<queue>

#include<stack>

#include <iomanip>

using namespace std;

#define LL long long

const int INF=0x3f3f3f3f;

const int N=106;

int head[N],I;

struct node

{

    double r,c;

    int j,next;

}side[N*2];

double dist[N];

void add(int i,int j,double r,double c)

{

    side[I].j=j;

    side[I].c=c;

    side[I].r=r;

    side[I].next=head[i];

    head[i]=I++;

}

bool spfa(int s,double k,int n)

{

    int num[N];

    bool in[N];

    memset(num,0,sizeof(num));

    memset(in,false,sizeof(in));

    queue<int>qt;

    for(int i=1;i<=n;++i)

    dist[i]=0.0;

    dist[s]=k;

    qt.push(s);

    num[s]=1;

    while(!qt.empty())

    {

        int x=qt.front();qt.pop();

        in[x]=false;

        if(x==s&&dist[x]>k)

        return true;

        for(int t=head[x];t!=-1;t=side[t].next)

        {

            int j=side[t].j;

            if(dist[j]<(dist[x]-side[t].c)*side[t].r)

            {

                dist[j]=(dist[x]-side[t].c)*side[t].r;

                while(!in[j])

                {

                    ++num[j];

                    if(num[j]>=n)

                    return true;

                    in[j]=true;

                    qt.push(j);

                }

            }

        }

    }

    return false;

}

int main()

{

    //freopen("data.in","r",stdin);

    int n,m,s;

    double k;

    while(cin>>n>>m>>s>>k)

    {

        memset(head,-1,sizeof(head));

        I=0;

        while(m--)

        {

            int a,b;

            double rab,cab,rba,cba;

            cin>>a>>b>>rab>>cab>>rba>>cba;

            add(a,b,rab,cab);

            add(b,a,rba,cba);

        }

        if(spfa(s,k,n))

        cout<<"YES"<<endl;

        else

        cout<<"NO"<<endl;

    }

    return 0;

}

 

你可能感兴趣的:(Exchange)