程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 2240 Arbitrage

POJ 2240 Arbitrage

編輯:C++入門知識

Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11539   Accepted: 4845
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. www.2cto.com

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

Ulm Local 1996
[Submit] [Go Back] [Status] [Discuss]

注意:開數組的時候一定要注意數據的類型,因為這個錯了很多次,到最後才注意到res數組開成整型的了

[cpp] 
#include <iostream> 
#include <string> 
using namespace std; 
int statck[1000000]; 
int status[100]; 
double res[100]; 
class num 

    public: 
    int end; 
    double val; 
    int next; 
}a[1000000]; 
int b[100]; 
int sum[100],tag,w; 
string s1[100]; 
int n; 
int main() 

    void deal(int k); 
    int i,j,m,s,t,x,y,z; 
    string s2,s3; 
    double val; 
    t=1; 
    while(cin>>n) 
    { 
        if(!n) 
        { 
            break; 
        } 
        for(i=1;i<=n;i++) 
        { 
            cin>>s1[i]; 
        } 
        cin>>m; 
        memset(b,-1,sizeof(b)); 
        z=0; 
        for(i=1;i<=m;i++) 
        { 
            cin>>s2; 
            cin>>val; 
            cin>>s3; 
            for(j=1;j<=n;j++) 
            { 
                if(s1[j]==s2) 
                { 
                    x=j; 
                } 
                if(s1[j]==s3) 
                { 
                    y=j; 
                } 
            } 
            a[z].end=y; a[z].val=val; 
            a[z].next=b[x]; 
            b[x]=z;  z++; 
        } 
        for(i=1;i<=n;i++) 
        { 
            tag=0; 
            deal(1); 
            if(tag==1) 
            { 
                break; 
            } 
        } 
        cout<<"Case "<<t<<":"<<" "; 
        t++; 
        if(i==n+1) 
        { 
            cout<<"No"<<endl; 
        }else 
        { 
            cout<<"Yes"<<endl; 
        } 
    } 
    return 0; 

void deal(int k) 

    int i,j,base,top; 
    int x; 
    for(i=1;i<=n;i++) 
    { 
       res[i]=0; 
    } 
    res[k]=100.0; 
    base=top=0; 
    memset(status,0,sizeof(status)); 
    memset(sum,0,sizeof(sum)); 
    statck[top++]=k; 
    status[k]=1; 
    sum[k]+=1; 
    w=0; 
    while(base<top) 
    { 
        x=statck[base]; 
        base++; 
        status[x]=0; 
        for(j=b[x];j!=-1;j=a[j].next) 
        { 
            if((res[x]*a[j].val)>res[a[j].end]) 
            { 
                res[a[j].end]=res[x]*a[j].val; 
                if(status[a[j].end]==0) 
                { 
                    sum[a[j].end]+=1; 
                    statck[top++]=a[j].end; 
                    status[a[j].end]=1; 
                    if(sum[a[j].end]>=n) 
                    { 
                        tag=1; 
                        break; 
                    } 
                } 
            } 
        } 
        if(j!=-1) 
        { 
            break; 
        } 
    } 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved