HDU 1217 Arbitrage Floyed

/*
 * F數組定義爲從i點到j點只可以使用[1, k]中的點所獲得的最大利潤。由於都是匯率換算
 * 這裏的更新操作應該是乘法運算。具體細節可以看代碼。
 */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e2 + 10;
const int MAXM = 1e5 + 10;

map<string, int> M;
int n, m;
double F[MAXN][MAXN];

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //cout << setiosflags(ios::fixed) << setprecision(1); //保留小數點後1位
    //cout << setprecision(1); //保留1位有效數字
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);

    int cas = 1;
    while(cin >> n){
        if(n == 0) break;
        for(int i = 0; i <= n; ++i)
            for(int j = 0; j <= n; ++j)
                F[i][j] = i == j ? 1.0 : 0.0;   //自己到自己的匯率爲1,其他爲0,因爲求的是最大值
        M.clear();
        for(int i = 1; i <= n; ++i){
            string name;
            cin >> name;
            M[name] = i;
        }
        cin >> m;
        for(int i = 1; i <= m; ++i){
            string strs, stre;
            double w;
            cin >> strs >> w >> stre;
            F[M[strs]][M[stre]] = w;
            //F[M[stre]][M[strs]] = 1.0 / w;    //邊是單向的。。。ennnn
        }
        for(int k = 1; k <= n; ++k)
            for(int i = 1; i <= n; ++i)
                for(int j = 1; j <= n; ++j)
                    F[i][j] = max(F[i][j], F[i][k] * F[k][j]);
        bool ok = false;
        for(int i = 1; i <= n; ++i)
            if(F[i][i] > 1.0) {
                ok = true;
                break;
            }
        if(ok) cout << "Case " << cas++ <<": Yes" << endl;
        else cout << "Case " << cas++ <<": No" << endl;
    }
    return 0;
}

 

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