#include <stdio.h> #include <string.h> #include <conio.h> #define N 10 //102
#define eps 1e-8 double r[N][N], c[N][N]; double v, dis[N]; int n;
int bellman_ford(int s, double v) { int i, j, k, mark; dis[s] = v; for(i=1; i<=n-1; i++) { mark = 0; for(j=1; j<=n; j++) { for(k=1; k<=n; k++) { if(dis[k] +eps< (dis[j] - c[j][k]) * r[j][k]) //改变的松弛操作 { dis[k] = (dis[j] - c[j][k]) * r[j][k]; mark = 1; } } } if(!mark) break; }
for(j=1; j<=n; j++) { for(k=1; k<=n; k++) { if(dis[k] +eps < (dis[j] - c[j][k]) * r[j][k]) { dis[k] = (dis[j] - c[j][k]) * r[j][k]; v = dis[s]; return 1; } } } v = dis[s]; return 0; }
int main() { int i, j; int m, s; int a, b; double r1, r2, c1, c2, vv;
freopen("in.txt", "r", stdin); scanf("%d%d%d%lf", &n, &m, &s, &v); for(i=1; i<=m; i++) { scanf("%d%d%lf%lf%lf%lf", &a, &b, &r1, &c1, &r2, &c2); r[a][b] = r1; c[a][b] = c1; r[b][a] = r2; c[b][a] = c2; } vv = v; if(bellman_ford(s, v)) //有正权环 printf("YES\n"); else if(v - vv > eps) //没有正权环,钱增值了 printf("YES\n"); else printf("NO\n"); //不能实现套汇
getch(); return 0; } memory : 336k
time: 0MS
|