bellman_ford判断正环
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=1000;
const double eps=1e-8;
typedef struct Edge
{
int u,v;
double r,c;
}Edge;
int nodenum,m,edgenum=0,origional;
Edge edge[maxn];
double dist[maxn],money;
void Add_Edge(int a,int b,double r1,double c1,double r2,double c2)
{
edge[edgenum].u=a;edge[edgenum].v=b;edge[edgenum].r=r1;edge[edgenum++].c=c1;
edge[edgenum].v=a;edge[edgenum].u=b;edge[edgenum].r=r2;edge[edgenum++].c=c2;
}
bool Bellman_ford()
{
memset(dist,0,sizeof(dist));
dist[origional]=money;
for(int i=1;i<nodenum;i++)
{
bool flag=false;
for(int j=0;j<edgenum;j++)
{
int U=edge[j].u,V=edge[j].v;
if((dist[U]-edge[j].c)>0&&(dist[V]<(dist[U]-edge[j].c)*edge[j].r))
{
flag=true;
dist[V]=(dist[U]-edge[j].c)*edge[j].r;
}
}
if(flag==false)
{
return false;
}
}
bool flag=false;
for(int j=0;j<edgenum;j++)
{
int U=edge[j].u,V=edge[j].v;
if((dist[U]-edge[j].c)>0&&(dist[V]<(dist[U]-edge[j].c)*edge[j].r))
{
flag=true; break;
}
}
return flag;
}
int main()
{
while(scanf("%d%d%d%lf",&nodenum,&m,&origional,&money)!=EOF)
{
edgenum=0;
for(int i=0;i<m;i++)
{
int a,b;double r1,c1,r2,c2;
scanf("%d%d%lf%lf%lf%lf",&a,&b,&r1,&c1,&r2,&c2);
Add_Edge(a,b,r1,c1,r2,c2);
}
if(Bellman_ford())
{
puts("YES");
}
else
{
puts("NO");
}
}
return 0;
}