• POJ-2240 Arbitrage( 最短路 )


    题目链接:http://poj.org/problem?id=2240

    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

    题目大意:已知货币之间的汇率,问是否有一种交换途径使得交换之后钱变多了
    解题思路:简单的判环题,直接Bellman_Ford

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<map>
     6 
     7 using namespace std;
     8 
     9 struct Edge{
    10     int beg, end;
    11     double cur;
    12 }edge[1000];
    13 
    14 int n, m;
    15 double dis[35];
    16 string str1, str2;
    17 
    18 bool relax( int beg, int end, double cur ){
    19     if( dis[beg] * cur > dis[end] ){
    20         dis[end] = dis[beg] * cur;
    21         return true;
    22     }
    23     return false;
    24 }
    25 
    26 bool Bellman_Ford(){
    27     for( int t = 1; t < n; t++ ){
    28         for( int i = 1; i <= m; i++ ){
    29             relax( edge[i].beg, edge[i].end, edge[i].cur );
    30         }
    31     }
    32 
    33     for( int i = 1; i <= m; i++ ){
    34         if( relax( edge[i].beg, edge[i].end, edge[i].cur ) ) return false;
    35     }
    36     return true;
    37 }
    38 
    39 int main(){
    40     ios::sync_with_stdio( false );
    41 
    42     int t = 1;
    43     while( cin >> n, n ){
    44         map< string, int > money;
    45         for( int i = 1; i <= n; i++ ){
    46             cin >> str1;
    47             money[str1] = i;
    48         }
    49 
    50         cin >> m;
    51         memset( dis, 0, sizeof( dis ) );
    52         dis[1] = 1;
    53         for( int i = 1; i <= m; i++ ){
    54             cin >> str1 >> edge[i].cur >> str2;
    55             edge[i].beg = money[str1];
    56             edge[i].end = money[str2];
    57         }
    58 
    59         cout << "Case " << t++ << ": ";
    60         if( Bellman_Ford() ) cout << "No
    ";
    61         else cout << "Yes
    ";
    62     }
    63 
    64     return 0;
    65 }

  • 相关阅读:
    ElementUI表格表头自定义添加checkbox,点击不生效问题
    同步个人vs code 习惯性配置gists 到码云(Gitee)
    el-select 设置宽度自适应上层div
    vue-element-template模板vue-cli3本地使用proxy解决跨域问题
    vue-cli3用axios+proxy简单解决在dev开发环境下的跨域问题
    windows下EasyMock的安装与部署遇到的各种问题(爬坑)
    git同步代码至github和gitee(码云)
    Git使用总结(包含Git Bash和Git GUI的使用)
    Leetcode No.53 Maximum Subarray(c++实现)
    Leetcode No.35 Search Insert Position(c++实现)
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5660100.html
Copyright © 2020-2023  润新知