poj1860
经典bellman-ford,目标是求解最长路,(a-c)*r前加负号则转化为求解最短路,更简单的是用求解最短路的方式求解最长路(这举可行是题目原因)
#include <stdio.h> #include <string.h> #define MAXN 300 #define INF -(1<<29) int n,m,s; double v; struct node { double r,c; }map[MAXN][MAXN]; double d[MAXN]; void bellman() { for(int i=1;i<=n;d[i++]=INF); d[s]=v; for(int k=1;k<=n-1;++k) for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) if(d[j]<(d[i]-map[i][j].c)*map[i][j].r) d[j]=(d[i]-map[i][j].c)*map[i][j].r; } int main() { while(scanf("%d %d %d %lf",&n,&m,&s,&v)==4) { for(int i=0,a,b;i<m;++i) { scanf("%d %d",&a,&b); scanf("%lf %lf %lf %lf",&map[a][b].r,&map[a][b].c,&map[b][a].r,&map[b][a].c); } bellman(); bool f=true; for(int i=1;i<=n;++i) for(int j=1;j<=n;++j) if(d[j]<(d[i]-map[i][j].c)*map[i][j].r) { f=false; goto answer; } answer: printf((f?"NO\n":"YES\n")); } return 0; }