您的位置:首页 > 其它

poj 2240 Arbitrage

2015-02-16 22:17 260 查看
<span style="background-color: rgb(255, 255, 255);"><strong><span style="font-size:18px;">这是一个货币交换问题,就是问你货币交换之后能不能赚到钱,我用数组存储,
G[i][i]存储的是第i种货币交换若干次得到的第i种货币的比率,如果大于1就赚了,
否则没有赚;
用FLOYED求最大回报
</span></strong></span><pre name="code" class="cpp">/***********************************************
 * Author: fisty
 * Created Time: 2015/2/16 21:44:34
 * File Name   : four_I.cpp
 *********************************************** */
#include <iostream>
#include <cstring>
#include <deque>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <algorithm>
using namespace std;
#define Debug(x) cout << #x << " " << x <<endl
#define Memset(x, a) memset(x, a, sizeof(x))
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef pair<int, int> P;
#define FOR(i, a, b) for(int i = a;i < b; i++)
#define MAX_N 1001
int n, m;
map<string, int> mp;

void ID(string s, int i){
    mp[s] = i;
}
double G[MAX_N][MAX_N];

int main() {
    //freopen("in.cpp", "r", stdin);
    cin.tie(0);
    ios::sync_with_stdio(false);
    int cnt = 1;
    while(cin >> n){
        if(!n) break;
        FOR(i, 1, n+1){
            string s;
            cin >> s;
            ID(s, i);
        }
        //自己给自己汇率为1,其他为0
        FOR(i, 1, n+1){
            FOR(j, 1, n+1){
                G[i][j] = 0;
                if(i == j){
                    G[i][j] = 1;
                }
            }
        }
        cin >> m;
        FOR(i, 0, m){
            string u, v;
            double cost;
            cin >> u >> cost >> v;
            int e1 = mp[u];
            int e2 = mp[v];
            G[e1][e2] = cost ;
        }
        for(int k = 1;k <= n; k++){
            for(int i = 1;i <= n; i++){
                for(int j = 1;j <= n; j++){
                    if(G[i][j] < G[i][k] * G[k][j])
                        G[i][j] = G[i][k] * G[k][j];
                }
            }
        }
        int ok = 0;
        for(int i = 1;i <= n; i++){
            if(G[i][i] > 1){
                ok = 1;
                break;
            }
        }
        cout << "Case " << cnt++ << ": ";
        if(ok) cout << "Yes" << endl;
        else cout << "No" <<endl;
    }
    return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: