题目链接: http://poj.org/problem?id=1860
找正环,找最长路,水题,WA了两天了。。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#include <stdio.h> #include <string.h> struct Edge { int u, v; double r, c; }edge[210]; int rear, n, m, s; double v, dist[110]; bool bellman_ford() { memset(dist, 0, sizeof(dist)); dist[s] = v; for(int i = 1; i < n; i++) for(int j = 0; j < rear; j++) if(dist[edge[j].v] < (dist[edge[j].u]-edge[j].c) * edge[j].r) dist[edge[j].v] = (dist[edge[j].u]-edge[j].c) * edge[j].r; for(int j = 0; j < rear; j++) if(dist[edge[j].v] < (dist[edge[j].u]-edge[j].c) * edge[j].r) return 1; return 0; } int main() { int a, b; double rab, cab, rba, cba; while(scanf("%d %d %d %lf", &n, &m, &s, &v) != EOF) { rear = 0; while(m--) { scanf("%d %d %lf %lf %lf %lf", &a, &b, &rab, &cab, &rba, &cba); edge[rear++] = (struct Edge){a, b, rab, cab}; edge[rear++] = (struct Edge){b, a, rba, cba}; } printf("%s\n", bellman_ford() ? "YES" : "NO"); } return 0; }