须要推断是否有正权环存在,Bellman-Ford算法就能够辣~
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Edge { int u,v; double r,c; }edge[210]; int n,m,s,k; double w; double d[105]; int bellmanford() { for(int i=1;i<=n;i++) d[i]=0.0; d[s]=w; int i; for(i=1;i<=n;i++) { bool flag = false; for(int j=1;j<k;j++) { int x=edge[j].u; int y=edge[j].v; if(d[y]<(d[x]-edge[j].c)*edge[j].r) { flag = true ; d[y] = (d[x]-edge[j].c)*edge[j].r; } } if(!flag) return false; } for(int i=1;i<k;i++) { if(d[edge[i].v]<((d[edge[i].u]-edge[i].c)*edge[i].r)) return true; } return false; } int main() { while(scanf("%d%d%d%lf",&n,&m,&s,&w)!=EOF) { int a,b; double rab,cab,rba,cba; k=1; while(m--) { scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba); edge[k].u=a; edge[k].v=b; edge[k].r=rab; edge[k].c=cab; k++; edge[k].u=b; edge[k].v=a; edge[k].r=rba; edge[k].c=cba; k++; } int ans=bellmanford(); if(ans) printf("YES\n"); else printf("NO\n"); } return 0; }