get到新姿势,最小割=最大流,来个大佬的PPT
这道题的做法是将st和1的xpy连,0的xpy和ed连,xpy之间jy连双向边,然后呢答案就是最小割。
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct node
{
int x,y,c,next,other;
}a[];int len,last[];
void ins(int x,int y,int c)
{
int k1,k2; len++;k1=len;
a[len].x=x;a[len].y=y;a[len].c=c;
a[len].next=last[x];last[x]=len; len++;k2=len;
a[len].x=y;a[len].y=x;a[len].c=;
a[len].next=last[y];last[y]=len; a[k1].other=k2;
a[k2].other=k1;
}
int st,ed;
int h[],list[];
bool bt_h()
{
memset(h,,sizeof(h));h[st]=;
int head=,tail=;list[]=st;
while(head!=tail)
{
int x=list[head];
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]==&&a[k].c>)
{
h[y]=h[x]+;
list[tail]=y;
tail++;
}
}
head++;
}
if(h[ed]==)return false;
return true;
}
int findflow(int x,int f)
{
if(x==ed)return f;
int s=;
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(h[y]==h[x]+&&a[k].c>&&s<f)
{
int t=findflow(y,min(a[k].c,f-s));
s+=t;a[k].c-=t;a[a[k].other].c+=t;
}
}
return s;
}
int main()
{
int n,m,x,y;
scanf("%d%d",&n,&m);
st=n+,ed=n+;
len=;memset(last,,sizeof(last));
for(int i=;i<=n;i++)
{
scanf("%d",&x);
if(x==)ins(st,i,);
else ins(i,ed,);
}
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
ins(x,y,);ins(y,x,);
} int ans=;
while(bt_h()==true)
{
ans+=findflow(st,);
}
printf("%d\n",ans);
return ;
}