# Arbitrage poj 2240 bellman-ford判圈应用

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<string>
using namespace std;
const int MAXN = 40;
struct Edge
{
int u,v;
double w;
};

Edge e[MAXN * MAXN];
double dis[MAXN];
int n,m;
bool bellman_ford(int v0)
{
memset(dis,0,sizeof(dis));
dis[v0] = 1;
for(int i = 1; i <= n;++i) // 松弛的边的次数
{
for(int j = 1;j <= m;++j)
{
dis[e[j].v] = max(dis[e[j].v],(dis[e[j].u] * e[j].w));
}
}
if(dis[v0] > 1)
{
return 1;
}
else
{
return 0;
}
}
int main()
{
int t = 1;
while(cin >> n && n)
{

map<string,int> M;
for(int i = 1; i <= n;++i)
{
string temp;
cin >> temp;
M[temp] = i;
}
cin >> m;
int cnt = 1;
int k = m;
while(k--)
{
string s1,s2;
double w;
cin >> s1 >> w >> s2;
e[cnt].u = M[s1];
e[cnt].v = M[s2];
e[cnt].w = w;
cnt++;
}
int flag = 0;
cout << "Case " << t++ << ": ";
for(int i = 1; i <= n; ++i)
{
bool ans;
ans = bellman_ford(i);
if(ans)
{
flag = 1;
cout << "Yes" << endl;
break;
}
}
if(!flag)
cout << "No" << endl;
}
return 0;
}

原文作者：Bellman - ford算法
原文地址: https://blog.csdn.net/AC_jie/article/details/79182872
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。