Poj 2240 Arbitrage

简介:

click here~~

                                        ***Arbitrage***


Time Limit: 1000MS  Memory Limit: 65536K 
Total Submissions: 17969  Accepted: 7597 

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

题目大意:这是一个汇率兑换问题,就相当于n个点,m条边,然后就是知道从I到j的汇率,看能不能赚钱:
解题思路:就是看能不能兑换回来的时候权值是否增加就行了,采用Floyd算法。。。
上代码:

/*
2015 - 8 - 14 下午
Author: ITAK

今日的我要超越昨日的我,明日的我要胜过今日的我,
以创作出更好的代码为目标,不断地超越自己。
*/
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn = 100;
double map[maxn][maxn];
int n, m;
char str[maxn][maxn],s1[maxn],s2[maxn];
//map初始化
void Init()
{
    for(int i=0; i<=n; i++)
        for(int j=0; j<=n; j++)
            if(i == j)
                map[i][j] = 1;
            else
                map[i][i] = 0;
}
//Floyd 算法
void Floyd()
{
    int i, j, k;
    for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
                if(map[i][k]*map[k][j] > map[i][j])
                    map[i][j] = map[i][k]*map[k][j];
}
int main()
{
    int cas = 1;
    while(cin>>n,n)
    {
        Init();
        for(int i=1; i<=n; i++)
            cin>>str[i];
        cin>>m;
        double x;
        for(int i=1; i<=m; i++)
        {
            int j, k;
            cin>>s1>>x>>s2;
            for(j=1; j<=n; j++)
                if(strcmp(str[j],s1) == 0)
                    break;
            for(k=1; k<=n; k++)
                if(strcmp(str[k],s2) == 0)
                    break;
            map[j][k] = x;
        }
        Floyd();
        cout<<"Case "<<cas++<<":"<<' ';
        int f;
        for(f=1; f<=n; f++)
            if(map[f][f] > 1)
                break;
        if(f > n)
            puts("No");
        else
            puts("Yes");
    }
    return 0;
}
目录
相关文章
|
6月前
poj 3298 数状数组
题目大意是一条大街上住着n个乒乓球爱好者,他们的水平高低用一个数值表示,他们经常举办比赛,比赛要三个人,一人当裁判。对裁判是有一定要求的,裁判的水平必须介于两选手之间且必须住他们中间,计算可以举办多少场比赛
23 0
|
存储
poj 2299 求逆序数
http://poj.org/problem?id=2299 #include using namespace std; int aa[500010],bb[500010]; long long s=0; void merge(int l,int m,int r) { ...
767 0
|
测试技术
poj-1218 THE DRUNK JAILER 喝醉的狱卒
自己去看看原题; 题目大意: 就是一个狱卒喝醉了,他第一趟吧所有的监狱都带开,第二趟把能把二整除的监狱关闭,第三趟操作能把三整除的监狱; 求最后能逃跑的罪犯数 输入第一个数是代表 测试数据组数 每个数据代表狱卒来回的次数 当作开关问题即可 #include using names...
959 0
|
JavaScript