• kuangbin专题四 : 最短路 I 题 Arbitrage


     
    kuangbin专题四 : 最短路 I 题  Arbitrage
    POJ 2240
    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

    题意:先给 货币的种类 数量n 种, 然后给出 n 种货币的名字 , 再给出 转换方式的数量 m , 之后 m 种 具体的 货币转换关系 。
       某一种转换关系 a b c , 表示 货币 a 乘以 比例 b 为可以 换到的 货币 c 的数量 。
       问是否存在正环。存在输出 Yes 否则输出 No

    思路: 数组存放 n 种货币名称 , 数组下标 表示 给货币 编号 。
        然后给其中一种货币的本金置为 1 ,其他为 0 , 然后用 bellman_ford 算法判断正环。

    1.bellman_ford()
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std ; 
    #define maxn 1000
    char str[maxn][maxn] ; 
    double dis[maxn] ; 
    struct node {
        int a , 
            b ; 
        double rate ; 
    };
    
    node edge[maxn] ; 
    int n , m ; 
    // 获得 货币 s 在 数组中的编号 
    int getpos(char *s){
        for(int i=1 ; i<=n ; i++){
            if(strcmp(str[i] , s  ) == 0 ){
                return i ; 
            }
        }
    }
    // 输入 && 建边 
    void init(){
        
        char a[maxn] , c[maxn] ; 
        double b ; 
        
        for(int i=1 ; i<=n ; i++){
            scanf(" %s" , str[i]) ; 
        }
        scanf("%d" , &m) ; 
        for(int i=1 ; i<=m ; i++){
            scanf("%s %lf %s" , a , &b , c ) ; 
            edge[i].a = getpos(a) ; 
            edge[i].b = getpos(c) ;
            edge[i].rate = b ; 
        }
    }
    // 判断正环 
    bool bellman_ford(){
        for(int i=1 ; i<=n ; i++){
            dis[i] = 0 ; 
        }
        dis[1] =1 ; 
        
        for(int i=1 ; i<=n ; i++){
            for(int j=1 ; j<=m ; j++){
                if(dis[edge[j].a] * edge[j].rate > dis[edge[j].b]){
                    dis[edge[j].b] = dis[edge[j].a] * edge[j].rate ; 
                    
                }
            }
        }
        
        for(int i=1 ; i<= m ;i++){
            if(dis[edge[i].a] * edge[i].rate > dis[edge[i].b]){
                return true ; 
            }
        }
        return false ; 
    }
    
    int main(){
        int times = 0 ; 
        while(~scanf("%d" , &n) && n ){
            
            init() ; 
            printf("Case %d: " , ++ times) ; 
            if(bellman_ford()){
                printf("Yes
    ") ; 
            } else {
                printf("No
    ") ; 
            }
        }
        
        return 0 ; 
    } 
     

     2.floyd()

    #include <iostream>  
    #include <string.h>  
    using namespace std;  
    #define MAXC 100  
    #define MAXV 50  
      
    double map[MAXV][MAXV];  
    int n,m;  
      
    void Input(){  
        char s[MAXV][MAXC],a[MAXC],b[MAXC];  
        int i,k,j;  
        double c;  
        for(i=0;i<=n;i++)  
            for(j=0;j<=n;j++)  
                if(i==j)  
                    map[i][j]=1;  
                else  
                    map[i][i]=0;  
        for(i=1;i<=n;i++) scanf("%s",s[i]);  
        scanf("%d
    ",&m);  
        for(i=1;i<=m;i++){  
            scanf("%s %lf %s",a,&c,b);  
            for(j=1;j<=n;j++)  
                if(!strcmp(s[j],a)) break;  
            for(k=1;k<=n;k++)  
                if(!strcmp(s[k],b)) break;  
            map[j][k]=c;  
        }  
    }  
      
    void floyd(){  
        int i,j,k;  
        for(k=1;k<=n;k++)  
            for(i=1;i<=n;i++)  
                for(j=1;j<=n;j++)  
                    if(map[i][k]*map[k][j]>map[i][j])  
                        map[i][j]=map[i][k]*map[k][j];  
    }  
      
    int main(){  
        int cas=1,i;  
        while(scanf("%d
    ",&n) && n){  
            Input();  
            floyd();  
            printf("Case %d: ",cas++);  
            for(i=1;i<=n;i++)  
                if(map[i][i]>1) break;  
            if(i>n) printf("No
    ");  
            else printf("Yes
    ");  
        }  
        return 0;  
    }  
  • 相关阅读:
    传参存在潜在危险值
    JWT Json Web Token
    异步编程初探:async和await
    异步 async/Await C#
    React+ES6+Dva(roadHog,Webpabk,Babel)+AntDesign 技术栈相关学习文档
    iOS App 内跳转到手机系统设置页面
    h5(WKWebView)和iOS之间的交互问题
    iOS 10需要设置的隐私权限
    iOS APP开发设置启动图片 Launch Image
    iOS开发中常用的手势---边缘手势
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/7795896.html
Copyright © 2020-2023  润新知