poj 2240 Arbitrage

这个题求是否经过兑换后钱能不能增加。

这题可以用bellman比较容易解决;

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<queue>

#include<set>

#include<cstring>

#include<string>

#include<vector>

using namespace std;

class Edge

{

    public:

        int S,E;

        double rate;    

}edge[1024];

bool bell_man( int N ,int M )

{

    double dis[50];

    for( int i = 0 ; i <= N ;i ++ )

         dis[i] = 1.0;

    int flag = 0;

    for( int i = 0; i < N +4 ; i++ )

    {

        flag = 0;

        for( int j =  0; j < M ;j ++ )

        {

           if( dis[edge[j].E] < dis[edge[j].S]*edge[j].rate )

           {

               dis[edge[j].E] = dis[edge[j].S]*edge[j].rate;

               flag = 1;

           }    

        }

        if( !flag ) return false; 

    }

    return true;

}

int main(  )

{

    string str[50],start,end;

    int N,M,S,E,Case=1;

    double rate;

    while( scanf( "%d",&N ),N )

    {

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

        {

            cin>>str[i];    

        }

        scanf( "%d",&M );

        for( int i =0 ; i < M ; i ++ )

        {

            cin>>start>>rate>>end;

            for(int j = 1 ; j <= N ; j++)

            {

                if( start == str[j] ) S = j;

                if( end == str[j] ) E = j;    

            }    

            edge[i].S= S ;edge[i].E = E;

            edge[i].rate = rate;

        }

        printf( "Case %d: ",Case++ );

        if( bell_man(N , M) ) printf( "Yes\n" );

        else printf( "No\n" );     

    }

    //system( "pause" );

    return 0;

}

 

你可能感兴趣的:(poj)