UVa436 - Arbitrage (II)

本文探讨了C++编程语言在算法与数据结构领域的应用,涵盖了从基本数据结构到高级算法的深入研究,旨在提高程序员解决问题的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <cstdio>
#include <map>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <algorithm>

using namespace std;

const int N = 35;
const double EPS = 1e-6;

#define REP(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) REP(i, 0, b)

double f[N][N];
map<string, int> strMap;
int n;

int idx(string &s);
bool floyd_warshall(int n);

int main()
{
    string s;
    int m;
    string s1, s2;
    double rate;
    int t = 1;

#ifndef ONLINE_JUDGE
    //freopen("d:\\OJ\\uva_in.txt", "r", stdin);
    ifstream cin("uva_in.txt");
#endif
    
    while (cin >> n, n) {
        strMap.clear();
        rep(i, n) {
            cin >> s;
            idx(s);
        }
        cin >> m;

        rep(i, n) {
            rep(j, n) {
                if (i == j) f[i][j] = 1;
                else f[i][j] = -1;
            }
        }

        rep(i, m) {
            cin >> s1 >> rate >> s2;  
            int n1 = strMap[s1], n2 = strMap[s2];
            f[n1][n2] = rate;
        }

        cout << "Case " << t++ << ": ";
        cout << (floyd_warshall(n) ? "Yes": "No") << endl;
    }
    return 0;
}

int idx(string &s)
{
    if (strMap.count(s)) return strMap[s];

    int ret = strMap.size();

    strMap[s] = ret;

    return ret;
}

bool floyd_warshall(int n)
{
    rep(k, n) {
        rep (i, n) {
            rep(j, n) {
                if (f[i][k] > 0 && f[k][j] > 0) {
                    f[i][j] = max(f[i][j], f[i][k] * f[k][j]);
                }
            }
        }
    }
    
    rep(i, n) {
        if (f[i][i] > 1) return true;
    }

    return false;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值