POJ 2240 Arbitrage - (Bellman-Ford)

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

#include <stdio.h> 
#include <vector> 
#include <algorithm>
#include <map>
#include <string>
using namespace std;

//POJ 2240 Arbitrage

typedef struct
{
    int src, dest;
    double rate;
}Edge;

int main()
{
    int n, _case = 0;
    while (~scanf("%d", &n))
    {
        if (n == 0)break;
        _case++;
        char buf[64];
        map<string, int> id;
        for (int i = 0; i < n; ++i)
        {
            scanf("%s", buf);
            id[buf] = i;
        }
        int m;
        scanf("%d", &m);
        vector<Edge> edges(m);
        for (int i = 0; i < m; ++i)
        {
            scanf("%s", buf);
            edges[i].src = id[buf];
            scanf("%lf", &(edges[i].rate));
            scanf("%s", buf);
            edges[i].dest = id[buf];
        }

        //bellman-ford
        bool flag = false;
        for (int k = 0; k < n; ++k)
        {
            //suppose k is source 
            vector<double> currency(n);
            currency[k] = 1.0;

            //for each edge in edges[m],
            //loose the src->dest
            for (int j = 0; j < n; ++j)
            {
                for (int i = 0; i < m; ++i)
                {
                    int src = edges[i].src;
                    int dest = edges[i].dest;
                    double rate = edges[i].rate;
                    //loose and update
                    if (currency[dest] < currency[src] * rate)
                        currency[dest] = currency[src] * rate;
                }
            }
            //judge whether there is a positive circle
            if (currency[k] > 1.0)
            {
                flag = true;
                break;
            }
        }
        if (flag)
            printf("Case %d: Yes\n", _case);
        else printf("Case %d: No\n", _case);
    }
    system("pause");
    return 0;
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/rsy56640/article/details/79874626
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞