poj 2240__Arbitrage (Floyd)

Arbitrage
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 18152   Accepted: 7676

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

Source

 
 
题意:给出货币间的汇率,问能否交换货币之后获利。
 
想法1:首先想到dfs,每次从一个货币开始搜索,设ans=1,每次把乘上货币的汇率,直到找到起始货币,如果ans>1,标记之后回溯,每次回溯的时候把ans还原。
结果:TLE。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int n,m,s,flag,vis[33][33];
double rt,ch[33][33],ans;
char ss[110],a[110],b[110];
map<string,int> ma;
void dfs(int aa)
{
    double ans1;
    for(int i=0;i<n;i++) {
        if(ch[aa][i]&&vis[aa][i]==0) {
            vis[aa][i]=1;
            ans1=ans;
            ans*=ch[aa][i];
            if(i==s&&ans>1) {
                flag=1;return ;
            }
            dfs(i);
            if(flag)
                return ;
            ans=ans1;
            vis[aa][i]=0;
        }
    }
}
int main()
{
    int k=1;
    while(~scanf("%d",&n)&&n) {
        for(int i=0;i<n;i++) {	把货币名称映射为数字,便于使用邻接矩阵。
            scanf("%s",ss);
            ma[ss]=i;
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++) {
            scanf("%s%lf%s",a,&rt,b);
            ch[ma[a]][ma[b]]=rt;
        }
        flag=0;
        memset(vis,0,sizeof(vis));
        for(s=0;s<n;s++) {
            ans=1.0;

            dfs(s);
        }
        if(flag==1) {
            printf("Case %d: Yes\n",k++);
        }
        else
            printf("Case %d: No\n",k++);
    }
    return 0;
}

想法2:问题的实质相当于求有环最长路径,用floyd处理是不错的选择。
结果:AC。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
int n,m,k=1;
double ch[33][33];
int main()
{
    double rt;
    map<string,int> ma;
    char ss[110],a[110],b[110];
    while(~scanf("%d",&n)&&n) {
        memset(ch,0,sizeof(ch));
        for(int i=0;i<n;i++) {
            scanf("%s",&ss);
            ch[i][i]=1;
            ma[ss]=i;
        }
        scanf("%d",&m);
        for(int i=0;i<m;i++) {
            scanf("%s%lf%s",a,&rt,b);
            ch[ma[a]][ma[b]]=rt;
        }
        for(int i=0;i<n;i++) {
            for(int j=0;j<n;j++) {
                for(int k=0;k<n;k++) {
                    if(ch[i][j]<ch[i][k]*ch[k][j]) {
                        ch[i][j]=ch[i][k]*ch[k][j];
                    }
                }
            }
        }
        int flag=0;
        for(int i=0;i<n;i++) {
            if(ch[i][i]>1) {
                flag=1;
                break;
            }
        }
        if(flag)
            printf("Case %d: Yes\n",k++);
        else
            printf("Case %d: No\n",k++);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章