Input
Output
Sample Input
3 2 1 20.0 1 2 1.00 1.00 1.00 1.00 2 3 1.10 1.00 1.10 1.00
Sample Output
YES
//题意简单,判断正环,原点值增加即为真
#include <iostream> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <stack> #include <stdio.h> #include <cmath> #include <string.h> #include <vector> #define ll long long using namespace std; int b[210][2], tol; double hy[210][2], dis[110]; bool bellman(int s, int n, double v) { for(int i = 1; i <= n; i++) dis[i] = 0; dis[s] = v; for(int i = 1; i <= n; i++) { bool flag = 0; for(int j = 0; j < tol; j++) { int x1 = b[j][0], x2 = b[j][1]; if(dis[x2] < (dis[x1] - hy[j][1]) * hy[j][0]) { flag = 1; dis[x2] = (dis[x1] - hy[j][1]) * hy[j][0]; } } if(flag == 0) return 0; } for(int i = 0; i < tol; i++) if(dis[b[i][1]] < (dis[b[i][0]] - hy[i][1]) * hy[i][0]) return 1; return 0; } int main() { ios::sync_with_stdio(false); double v; int n, m, s; while(~scanf("%d%d%d%lf", &n, &m, &s, &v)) { tol = 0; while(m--) { int a1, a2; double r1, c1, r2, c2; scanf("%d%d%lf%lf%lf%lf", &a1, &a2, &r1, &c1, &r2, &c2); b[tol][0] = a1, b[tol][1] = a2; hy[tol][0] = r1, hy[tol][1]= c1; tol++; b[tol][0] = a2, b[tol][1] = a1; hy[tol][0] = r2, hy[tol][1] = c2; tol++; } if(bellman(s, n, v)) printf("YES\n"); else printf("NO\n"); } return 0; }
原文:https://www.cnblogs.com/zxybdnb/p/11939410.html