http://poj.org/problem?id=1860
模板提
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
#define INF 0xfffffff
double dis[],v;
int n,m;
struct node
{
int r,c;
double r1,c1,r2,c2;
}p[];
int bell_ford(int s)
{
int i,j;
memset(dis,,sizeof(dis));
dis[s] = v;
for(i = ; i <= n ; i++)
{
int flag = ;
for(j = ; j <= m ; j++)
{
if((dis[p[j].r]-p[j].c1)*p[j].r1>dis[p[j].c])
dis[p[j].c] = (dis[p[j].r]-p[j].c1)*p[j].r1;
if((dis[p[j].c]-p[j].c2)*p[j].r2>dis[p[j].r])
dis[p[j].r] = (dis[p[j].c]-p[j].c2)*p[j].r2;
flag = ; }
if(!flag) break;
}
for(j = ; j <= m ; j++)
{
if((dis[p[j].r]-p[j].c1)*p[j].r1>dis[p[j].c])
return ;
if((dis[p[j].c]-p[j].c2)*p[j].r2>dis[p[j].r])
return ;
}
return ;
}
int main()
{
int i,s;
while(scanf("%d%d%d%lf",&n,&m,&s,&v)!=EOF)
{
for(i = ; i <= m ; i++)
{
scanf("%d%d%lf%lf%lf%lf",&p[i].r,&p[i].c,&p[i].r1,&p[i].c1,&p[i].r2,&p[i].c2);
}
if(bell_ford(s))
{
puts("YES");
}
else
puts("NO");
}
return ;
}