poj 2240 Arbitrage(bellman-ford)

和poj 1860 差不多,求正权回路

#include <iostream>
#include <string>
#include <map>
#include <cstring>
using namespace std;

struct node
{
    int a,b;
    double r;
};

node edges[1000];
double dis[100];
map<string,int> relate;
int n,m;

bool bellmanFord()
{
    memset(dis,0,sizeof(dis));
    dis[0] = 10;
    for(int i = 0; i < n; ++i)
    {
        for(int j = 0; j < m; ++j)
        {
            if(dis[edges[j].b] < dis[edges[j].a]*edges[j].r)
            {
                dis[edges[j].b] = dis[edges[j].a]*edges[j].r;
                if(i == n-1)return true;
            }
        }
    }
    return false;
}

int main()
{
    int time = 0;
    while(cin >> n && n)
    {
        ++time;
        string str;
        for(int i = 0; i < n; ++i)
        {
            cin >> str;
            relate[str] = i;
        }
        cin >> m;
        string str1,str2;
        double rate;
        for(int i = 0; i < m; ++i)
        {
            cin >> str1 >> rate >> str2;
            edges[i].a = relate[str1];
            edges[i].b = relate[str2];
            edges[i].r = rate;
        }
        if(bellmanFord())
            cout << "Case " << time << ": Yes" << endl;
        else
            cout << "Case " << time << ": No" << endl;
    }
    return 0;
}
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/gyhguoge01234/article/details/54809678
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞