码迷,mamicode.com
首页 > 其他好文 > 详细

Arbitrage

时间:2014-10-10 22:34:44      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:http   io   os   ar   for   sp   div   art   on   

点击打开链接

题意:货币兑换,换取最大钱币;

解析:构图,spfa

#include<iostream>
#include<cstring>
#include<queue>
#include<map>
#include<cstdio>

using namespace std;

const int maxn = 1005;
double cost[ maxn ][ maxn ], dis[ maxn ];
int vis[ maxn ];
int n, m;
char ch[ maxn ], str1[ maxn ], str2[ maxn ];
map<string,int> str;


int spfa( int start ){
    for( int i = 1; i <= n; ++i ){
        dis[ i ] = vis[ i ] = 0;
    }
    vis[ start ] = 1;     
    dis[ start ] = 1.0;
    queue< int > Q;
    while( !Q.empty() ){
        Q.pop();
    }
    Q.push( start );
    while( !Q.empty() ){
        int p = Q.front();
        Q.pop();
        vis[ p ] = 0;
        for( int i = 1; i <= n; ++i ){
            if( dis[ p ] * cost[ p ][ i ] > dis[ i ] ){
                dis[ i ] = dis[ p ] * cost[ p ][ i ];
                if( dis[ start ] > 1.0 ){
                    return 1;
                }
                if( !vis[ i ] ){
                    vis[ i ] = 1;
                    Q.push( i );
                }    
            }
        }
    }
    return 0;
}


int main(){
    int Case = 1;
    while( scanf( "%d", &n ) != EOF ){
        if( !n )
            break;
        str.clear();
        for( int i = 1; i <= n; ++i ){
            for( int j = 1; j <= n; ++j ){
                if( i == j )
                    cost[ i ][ j ] = 1.0;
                else
                    cost[ i ][ j ] = 0;
            }
        }
        for( int i = 1; i <= n; ++i ){
            scanf( "%s", ch );
            str[ ch ] = i;
        }
        scanf( "%d", &m );
        double num;
        for( int i = 1; i <= m; ++i ){
            scanf( "%s%lf%s", str1, &num, str2 );
            cost[ str[ str1 ] ][ str[ str2 ] ] = num;
        }
        int flag = 0;
        for( int i = 1;i <= n; ++i ){
            if( spfa( i ) ){
                flag = 1;
                break;
            }
        }
        printf( "Case %d: ", Case++ );
        if( flag ){
            puts( "Yes" );
        }
        else{
            puts( "No" );
        }
    }
    return 0;
}


Arbitrage

标签:http   io   os   ar   for   sp   div   art   on   

原文地址:http://www.cnblogs.com/hrhguanli/p/4017871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!