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;
}
目录
相关文章
|
存储
|
C语言
poj 2503 查字典
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language.
870 0
POJ-1003-hangover
Description How far can you make a stack of cards overhang a table? If you have one card, you can create a maximum overhang of half a card length.
769 0
|
算法 数据建模 机器学习/深度学习
poj1273Drainage Ditches
1 #include 2 /* 3 题意:就是寻找从源点到汇点的最大流! 4 要注意的是每两个点的流量可能有多个,也就是说有重边,所以要把两个点的所有的流量都加起来 5 就是这两个点之间的流量了! 6 ...
853 0