poj 2240 Arbitrage

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   java   io   strong   

Time Limit: 1000 MS Memory Limit: 65536 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

DescriptionArbitrage 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. InputThe 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. OutputFor 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
3USDollarBritishPoundFrenchFranc3USDollar 0.5 BritishPoundBritishPound 10.0 FrenchFrancFrenchFranc 0.21 USDollar3USDollarBritishPoundFrenchFranc6USDollar 0.5 BritishPoundUSDollar 4.9 FrenchFrancBritishPound 10.0 FrenchFrancBritishPound 1.99 USDollarFrenchFranc 0.09 BritishPoundFrenchFranc 0.19 USDollar0
Sample Output
Case 1: YesCase 2: No
題意:對於每一個頂點構成的迴路 兌換率是否大於1
 #include <iostream> #include <string.h> #include <stdio.h> using namespace std; #define maxx 35 #define maxn 1000 char name[maxx][20],a[20],b[20];  ///鏈表進行儲存 double maxdis[maxn];  ///類似於dis[]數組  不過這次求最大迴路 double x; ///兌換率 int n,t; int falg; struct exchange {     int ci,cj;     double cij; }ex[maxn]; void Bellman(int v0) {     falg=0;     memset(maxdis,0,sizeof(maxdis));     maxdis[v0]=1;     for(int k=1;k<=n;k++)  ///要尋找迴路  所以從maxdis[0]遞推maxdis[1]....maxdis[n]     {         for(int i=0;i<t;i++)  ///每條邊加入是否值變大使最大         {             if(maxdis[ex[i].ci]*ex[i].cij>maxdis[ex[i].cj])  ///是程的關係了~~~             {                 maxdis[ex[i].cj]=maxdis[ex[i].ci]*ex[i].cij;  ///求最大的  遇到大的就更新             }         }     }     if(maxdis[v0]>1)     falg=1; } int main() {     int num;     int casee=0;     while(scanf("%d",&n),n)     {         for(int i=0;i<n;i++)         {             cin>>name[i];             //scanf("%s",name[num]);         }         int i,j,k;         scanf("%d",&t);         for(i=0;i<t;i++)         {             cin>>a>>x>>b;             //scanf("%s%if%s",a,&x,b);             for(j=0;strcmp(a,name[j]);j++); ///,是空迴圈  沒有迴圈語句的意思             for(k=0;strcmp(b,name[k]);k++);  ///將字串變成了數字  對應關係             ex[i].ci=j;             ex[i].cij=x;             ex[i].cj=k;             ///cout<<i<<‘ ‘<<j<<‘ ‘<<k<<"!!!!!!!"<<endl;  輸出一次啊   挺神奇的東東         }         for(int i=0;i<n;i++)         {             Bellman(i);  ///遍曆每一個點的迴路             if(falg)  ///如果出現兌換率〉1的現象  標記直接退出就好             break;         }         if(falg)         printf("Case %d: Yes\n",++casee);         else         printf("Case %d: No\n",++casee);     }     return 0; }

 

poj 2240 Arbitrage

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.