和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;
}