• poj 2240Arbitrage


    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

    这题跟Currency Exchange有些类似,都是求一定兑换之后总量能不能增加,可以用bellman-ford解决
    不过我一开始用了floyd的变形,n的值较小- -
    floyd
     1 #include <stdio.h>
     2 #include <string.h>
     3 char map[31][50];
     4 int n;
     5 double dis[31][31];
     6 int search(char c[])
     7 {
     8     int i;
     9     for(i=0;i<n;i++)
    10         if(strcmp(c,map[i])==0)
    11             return i;
    12 }
    13 void floyd()
    14 {
    15     int i,j,k;
    16     for(k=0;k<n;k++)
    17         for(i=0;i<n;i++)
    18             for(j=0;j<n;j++)
    19                 if(dis[i][j]<dis[i][k]*dis[k][j])
    20                     dis[i][j]=dis[i][k]*dis[k][j];
    21 }
    22 int main()
    23 {
    24     int i,j,m,cas=1;
    25     double r;
    26     char s[50],t[50];
    27     while(scanf("%d",&n)&&n)
    28     {
    29         printf("Case %d: ",cas++);
    30         for(i=0;i<n;i++)
    31         {
    32             scanf("%s",map[i]);
    33             for(j=0;j<n;j++)
    34                 dis[i][j]=1;
    35         }
    36         scanf("%d",&m);
    37         while(m--)
    38         {
    39             scanf("%s%lf%s",s,&r,t);
    40             dis[search(s)][search(t)]=r;
    41         }
    42         floyd();
    43         for(i=0;i<n;i++)
    44             if(dis[i][i]>1)
    45             {
    46                 printf("Yes\n");
    47                 break;
    48             }
    49         if(i==n)
    50             printf("No\n");
    51     }
    52     return 0;
    53 }
    网上的bellman
     1 #include<iostream> 
     2 #include<string> 
     3 #include<map> 
     4 #define maxn 40 
     5 using namespace std; 
     6 map<string,int> mp; 
     7 struct Edge 
     8 { 
     9        int v,u; 
    10        double w; 
    11 } e[1005]; 
    12 int size,n; 
    13 double dist[maxn]; 
    14 void AddEdge(int a,int b,double c) 
    15 { 
    16      e[size].u=a; 
    17      e[size].v=b; 
    18      e[size].w=c; 
    19      size++; 
    20 } 
    21 bool Bellman_Ford() 
    22 { 
    23      int i,j; 
    24      for( i=1; i<=n; i++){ 
    25           bool flag=false; 
    26           for( j=0; j<size; j++){ 
    27                // cout<<e[j].u<<' '<<e[j].w<<endl; 
    28                if( dist[e[j].u]*e[j].w>dist[e[j].v]){ 
    29                    dist[e[j].v]=dist[e[j].u]*e[j].w; 
    30                    flag=true; 
    31                } 
    32           } 
    33           if( !flag) break; 
    34           if( i==n&&flag) return true; 
    35      } 
    36      return false; 
    37 } 
    38 int main() 
    39 { 
    40     int m,ca,i; 
    41     double c; 
    42     string str1,str2; 
    43     ca=0; 
    44     while( cin>>n&&n){ 
    45            ++ca; 
    46            mp.clear(); 
    47            for( i=1; i<=n; i++){ 
    48                 cin>>str1; 
    49                 mp[str1]=i; 
    50            } 
    51            cin>>m; 
    52            memset(e,0,sizeof(e)); 
    53            size=0; 
    54            while( m--){ 
    55                   cin>>str1>>c>>str2; 
    56                   AddEdge(mp[str1],mp[str2],c); 
    57            } 
    58           memset(dist,0,sizeof(dist)); 
    59                 dist[1]=1; 
    60            cout<<"Case "<<ca<<": "; 
    61            if( Bellman_Ford()) 
    62                cout<<"Yes"<<endl; 
    63            else 
    64                cout<<"No"<<endl; 
    65     }  
    66 } 
     
  • 相关阅读:
    高斯核函数的代码体现
    程序编译
    DoH
    随笔1
    获取节点值的方式
    DOM解析XML
    URLConnection发送请求,并接收数据
    myeclipse编译后的jsp文件存放位置
    各种中文乱码
    各种提交的区别
  • 原文地址:https://www.cnblogs.com/wilsonjuxta/p/2963768.html
Copyright © 2020-2023  润新知