标签:
/*
*POJ 2240 Arbitrage
*求从源点v0出发 到各点(包括其本身)最长路径(权值乘积)
*
*/
#include <cstdio>
#include <cstring>
#define MAXN 50 //顶点数最大值
#define MAXM 1000 //边最大值
#define max(a, b) ((a) > (b) ? (a) : (b))
struct exchange {
int ci, cj;
double cij;
} ex[MAXM]; //汇率关系
int i, j, k;
int n, m; //货币种类 汇率数目
char name[MAXN][20], a[20], b[20]; //货币名称
double x; //汇率
double maxdist[MAXN]; //源点i到其他每个顶点(包括本身)的最长路径长度
int flag; //存在套汇标志
int kase = 0;
int readcase()
{
scanf("%d", &n);
if (n == 0) {
return 0;
}
for (i = 0; i < n; i++) {
scanf("%s", name[i]);
}
scanf("%d", &m);
for (i = 0; i < m; i++) {
scanf("%s %lf %s", a, &x, b);
for (j = 0; strcmp(a, name[j]); j++) ;
for (k = 0; strcmp(b, name[k]); k++) ;
ex[i].ci = j;
ex[i].cij = x;
ex[i].cj = k;
}
return 1;
}
void bellman(int v0)
{
flag = 0;
memset(maxdist, 0, sizeof(maxdist));
maxdist[v0] = 1;
for (k = 1; k <= n; k++) {
for (i = 0; i < m; i++) { //判断每条边 加入它是否能使最大距离增加
if (maxdist[ex[i].ci] * ex[i].cij > maxdist[ex[i].cj]) {
maxdist[ex[i].cj] = maxdist[ex[i].ci] * ex[i].cij;
}
}
}
if (maxdist[v0] > 1.0) {
flag = 1;
}
}
int main(void)
{
while (readcase()) {
for (i = 0; i < n; i++) {
bellman(i);
if (flag) {
break;
}
}
if (flag) {
printf("Case %d: Yes\n", ++kase);
} else {
printf("Case %d: No\n", ++kase);
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/subrshk/p/4244115.html