bzoj2768: [JLOI2010]冠军调查(双倍经验最小割)

2768: [JLOI2010]冠军调查

题目:传送门 

题解:
   双倍经验(1934

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define qread(x) x=read()
using namespace std;
inline int read()
{
    int f=1,x=0;char ch;
    while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return f*x;
}
struct node
{
    int x,y,c,next,other;
}a[210000];int len,last[110000];
int n,m,st,ed;
void ins(int x,int y,int c)
{
    int k1,k2;
    k1=++len;
    a[len].x=x;a[len].y=y;a[len].c=c;
    a[len].next=last[x];last[x]=len;
    
    k2=++len;
    a[len].x=y;a[len].y=x;a[len].c=0;
    a[len].next=last[y];last[y]=len;
    
    a[k1].other=k2;
    a[k2].other=k1;
}
int list[110000],h[110000],head,tail;
bool bt_h()
{
    memset(h,0,sizeof(h));h[st]=1;
    list[1]=st;head=1;tail=2;
    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]==0 && a[k].c>0)
            {
                h[y]=h[x]+1;
                list[tail++]=y;
            }
        }
        head++;
    }
    if(h[ed]>0)return true;
    return false;
}
int find_flow(int x,int flow)
{
    if(x==ed)return flow;
    int s=0,t;
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(h[y]==h[x]+1 && a[k].c>0 && s<flow)
        {
            s+=t=find_flow(y,min(a[k].c,flow-s));
            a[k].c-=t;a[a[k].other].c+=t;
        }
    }
    if(s==0)h[x]=0;
    return s;
}
int main()
{
    qread(n);qread(m);
    len=0;memset(last,0,sizeof(last));
    st=110000-2;ed=st+1;
    for(int i=1;i<=n;i++)
    {
        int x;qread(x);
        if(x==1)ins(st,i,1);
        else ins(i,ed,1);
    }
    for(int i=1;i<=m;i++)
    {
        int x,y;
        qread(x);qread(y);
        ins(x,y,1);
        ins(y,x,1);
    }
    int ans=0;
    while(bt_h())ans+=find_flow(st,999999999);
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2018-03-03 15:08  CHerish_OI  阅读(143)  评论(0编辑  收藏  举报