点点滴滴”

导航

poj 2240 Arbitrage

Time Limit: 1000 MS Memory Limit: 65536 KB

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

[Submit] [Status] [Discuss]

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

题意:对于每一个顶点构成的回路 兑换率是否大于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;
 }

 

posted on 2014-08-26 19:36  点点滴滴”  阅读(141)  评论(0编辑  收藏  举报