Arbitrage---poj2240(floyd)

题目链接:http://poj.org/problem?id=2240

题意:有n个国家的,有m个关系,每个关系的格式是:A B C表示1单位的A国货币可以换B单位C国货币;求是否存在一种方法使得货币升值;

就是找到一个环使得自己到自己的距离大于1即可;

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <algorithm>
#include <map>
#include <string>
typedef long long LL;
#define INF 0x3f3f3f3f
#define N 1100

using namespace std;

map<string, int> Mp;
double G[N][N];
int n;

void floyd()
{
    for(int k=1; k<=n; k++)
    for(int i=1; i<=n; i++)
    for(int j=1; j<=n; j++)
    {
        G[i][j] = max(G[i][j], G[i][k]*G[k][j]);
    }
}


int main()
{
    int t = 1, m;
    while(scanf("%d", &n), n)
    {
        char s1[110], s2[110], s[110];
        Mp.clear();
        memset(G, 0, sizeof(G));

        int cnt = 1;
        for(int i=1; i<=n; i++)
        {
            scanf("%s", s);
            Mp[s] = cnt++;
        }
        scanf("%d", &m);
        for(int i=1; i<=m; i++)
        {
            int u, v; double w;
            scanf("%s %lf %s", s1, &w, s2);
            u = Mp[s1]; v = Mp[s2];
            G[u][v] = w;
        }
        floyd();
        int flag = 0;
        for(int i=1; i<=n; i++)
        {
            if(G[i][i] > 1.0)
            {
                flag = 1;
                break;
            }
        }
        if(flag)printf("Case %d: Yes
", t++);
        else printf("Case %d: No
", t++);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5813268.html