zoukankan      html  css  js  c++  java
  • POJ 2240 Arbitrage

    Arbitrage
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 16175   Accepted: 6803

    Description

    Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 * 10.0 * 0.21 = 1.05 US dollars, making a profit of 5 percent. 

    Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not. 

    Input

    The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible. 
    Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.

    Output

    For each test case, print one line telling whether arbitrage is possible or not in the format "Case case: Yes" respectively "Case case: No".

    Sample Input

    3
    USDollar
    BritishPound
    FrenchFranc
    3
    USDollar 0.5 BritishPound
    BritishPound 10.0 FrenchFranc
    FrenchFranc 0.21 USDollar
    
    3
    USDollar
    BritishPound
    FrenchFranc
    6
    USDollar 0.5 BritishPound
    USDollar 4.9 FrenchFranc
    BritishPound 10.0 FrenchFranc
    BritishPound 1.99 USDollar
    FrenchFranc 0.09 BritishPound
    FrenchFranc 0.19 USDollar
    
    0
    

    Sample Output

    Case 1: Yes
    Case 2: No
    

    Source

    将字符串用map处理一下。

    然后用spfa判一下环就可以了~

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <stack>
    #include <algorithm>
    using namespace std;
    
    typedef long long LL;
    const int N = 110;
    const int inf = 1e7+7;
    const double PI = acos(-1.0);
    const double eps = 1e-6 ;
    
    int n , m ;
    struct node {
        int v ;
        double w ;
        node(){}
        node( int a , double b ) { v = a , w = b ; }
    };
    
    vector<node>g[N];
    map<string,int>mp;
    string str1 , str2 ;
    bool vis[N] , inq[N] ;
    double dis[N];
    int in_cnt[N];
    
    bool spfa( int s ) {
    
        queue<int>que;
        for( int i = 0 ; i <= n ; ++i ) dis[i] = 0.0 , in_cnt[i] = 0 , inq[i] = false;
        dis[s] = 1.0 ; inq[s] = true ; que.push(s);
        while( !que.empty() ) {
            int u = que.front() ; que.pop();
            inq[u] = false , vis[u] = true ;
            for( int i = 0 ; i < g[u].size() ; ++i ) {
                int v  = g[u][i].v ; double w = g[u][i].w ;
                if( dis[u] * w > dis[v] ) {
                    dis[v] = dis[u] * w ;
                    if( !inq[v] ) {
                        in_cnt[v] ++ ;
                        if( in_cnt[v] >= n ) return false;
                        inq[v] = true; que.push(v);
                    }
                }
            }
        }
        return true;
    }
    
    void run()
    {
        double w ;
        mp.clear(); for( int i = 0 ; i <= n ; ++i )g[i].clear();
        for( int i = 1 ; i <= n ; ++i ){
            cin >> str1 , mp[str1] = i ;
        }
        cin >> m ;
        while( m-- ) {
            cin >> str1 >> w >> str2 ;
            int u = mp[str1]  , v = mp[str2] ;
            g[u].push_back( node(v,w) );
        }
        memset(vis,false,sizeof vis);
        bool tag = false ;
        for( int i = 1 ; i <= n ; ++i ) if( !vis[i] ) {
            if( !spfa(i) ) { tag = true ; break ; }
        }
        if( tag ) cout << "Yes" <<endl;
        else cout << "No" << endl;
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
        ios::sync_with_stdio(false);
        int cas = 1 ;
        while( cin >> n && n ){
            cout << "Case "<<cas++ <<": ";run();
        }
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    工厂模式(Factory)(转)
    好东西要随时记录下来
    Windows Server 2003服务器清理C盘空间的资料多个解决方法
    phonegap 开发指南系列开始之前(1)
    phonegap 开发指南系列简介(2)
    VBS脚本完美实现开机延时启动
    需求分析师入门
    phonegap 开发指南系列(3) 在Eclipse中Android开发环境搭建
    Win 2003 服务器安全基本设置
    Linux命令行与图形界面切换方法
  • 原文地址:https://www.cnblogs.com/hlmark/p/4099219.html
Copyright © 2011-2022 走看看