bzoj3436: 小K的农场:http://www.lydsy.com/JudgeOnline/problem.php?id=3436
差分约束系统裸题
建边然后问跑最长路就ok (建边可以自己推导一下)
其实觉得比糖果水。。。 (bzoj2330糖果:https://blog.csdn.net/qq_36038511/article/details/79674492)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int x,y,c,next;
}a[31000];
int last[11000],len;
int sta[11000000],top;
int d[11000],ru[11000];
bool v[11000];
void build(int x,int y,int c)
{
len++;
a[len].x=x;a[len].y=y;a[len].c=c;a[len].next=last[x];last[x]=len;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
//y=x+c
int u,x,y,c;
scanf("%d",&u);
if (u==1) {scanf("%d%d%d",&x,&y,&c); build(x,y,-c);} //a>=b+c b<=a-c
if (u==2) {scanf("%d%d%d",&x,&y,&c); build(y,x,c);build(x,y,0);} // 0<=a-b<=c a<=b+c b<=a
if (u==3) {scanf("%d%d",&x,&y); build(x,y,0);build(y,x,0);} //a=b
}
for (int i=n;i>0;i--) build(0,i,1);
memset(v,false,sizeof(v));
memset(d,-0x3f,sizeof(d));
memset(ru,0,sizeof(ru));
for (int i=n;i>=0;i--)
{
sta[++top]=i;
ru[i]++;v[i]=true;
}
bool bk=false;
while (top>0)
{
int x=sta[top--];
for (int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if (d[y]>d[x]+a[k].c)
{
d[y]=d[x]+a[k].c;
if (v[y]==false)
{
v[y]=true;
sta[++top]=y;
ru[y]++;
if (ru[y]>n) {bk=true;break;}
}
}
}
if (bk) break;
v[x]=false;
}
if (bk) printf("No\n"); else printf("Yes\n");
return 0;
}