hdu 1217 Arbitrage

傳送門

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 file 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

【題意】

現在你有一些錢,現在你可以將你的錢按照國際轉化規則轉換成其他的錢,並且沒有手續費,問你能否經過轉換將你的錢變多。

【分析】

題意挺有意思的不知道現實中可不可以。其實稍微分析一下就可以發現,這個數據量很小,可以直接暴力每個關係,然後不斷的轉換,然後判斷你的錢是多了還是少了。如果多了就直接輸出,否則的話就一直轉換。你可能會問,一直轉換不就死循環了嗎?其實這個很容易解釋,假使你每次轉換錢都變少了,那麼轉換一次之後就不會轉換了。因爲這個變少的值我們是不會選擇的。

【代碼】

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<cmath>
#include<map>
#include<time.h>
#include<bits/stdc++.h>
using namespace std;
double win[35][35];
double now[35];
string a,b;
map<string,int>mat;
int main()
{
//    freopen("in.txt","w",stdout);
    int len;
    int g_case = 1;
    while(cin>>len,len)
    {
        cout<<"Case "<< g_case++ <<": ";
        mat.clear();
        memset(win,0,sizeof(win));
        memset(now,0,sizeof(now));
        for(int i = 1;i<=len;i++)
        {
            cin>>a;
            mat[a] = i;
        }
        double xx;
        int x;
        cin>>x;
        while(x--)
        {
            cin>>a>>xx>>b;
            win[mat[a]][mat[b]] = xx;
        }
        now[1] = 1;
        bool flag = true;
        while(flag)
        {
            flag = false;
            for(int i = 1;i<=len;i++)
                for(int j = 1;j<=len;j++)
                    if(now[j]<now[i]*win[i][j])
            {
                flag = true;
                now[j] = now[i]*win[i][j];
            }
            if(now[1]>1) break;
        }
        if(now[1]>1) puts("Yes");
        else puts("No");
    }
    return 0;
}




發佈了182 篇原創文章 · 獲贊 5 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章