P1993-小K的农场【差分约束,SPFA】

版权声明:原创,未经作者允许禁止转载 https://blog.csdn.net/Mr_wuyongcong/article/details/88367831

正题

题目链接:https://www.luogu.org/problemnew/show/P1993


题目大意

有若干个条件

  1. W a + w > W b W_a+w>W_b
  2. W a + w < W b W_a+w<W_b
  3. W a = W b W_a=W_b

解题思路

差分约束不解释


c o d e code

#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int N=11000;
queue<int> q;
struct line{
    int to,w,next;
}a[N*4];
int n,m,tot,x,y,w,f[N],ls[N],len[N];
bool v[N];
void addl(int x,int y,int w)
{
    a[++tot].to=y;a[tot].w=w;
    a[tot].next=ls[x];ls[x]=tot;
}
bool spfa(int x){
	v[x]=1;
	for(int i=ls[x];i;i=a[i].next)
	{
		int y=a[i].to;
		if(f[y]>=f[x]+a[i].w) continue;
		f[y]=f[x]+a[i].w;
		if(v[y]) return 0;
		if(!spfa(y)) return 0; 
	}
	v[x]=0;
	return 1;
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++)
	{
		int S,x,y,w;
		scanf("%d%d%d",&S,&x,&y);
		if(S==1)
		{
			scanf("%d",&w);
			addl(y,x,w);
		}
		if(S==2)
		{
			scanf("%d",&w);
			addl(x,y,-w);
		}
		if(S==3)
		{
			addl(x,y,0);
			addl(y,x,0);
		}
	}
	for(int i=1;i<=n;i++)
	  addl(0,i,0),f[i]=-6666666;
	if(spfa(0)) printf("Yes");
	else printf("No");
}

猜你喜欢

转载自blog.csdn.net/Mr_wuyongcong/article/details/88367831