BZOJ2768: [JLOI2010]冠军调查

【传送门:BZOJ1934


简要题意:

  给出n个小盆友,有m对小伙伴关系,现在老师想要决定中午睡不睡觉,每个小盆友一开始都有自己的意愿,1表示支持,0表示反对,但是如果两个小伙伴的意愿不同的话,会发生冲突,所以小盆友可以改变自己开始的意愿来保持小伙伴友好的关系,但是这样子自己心里会有冲突

  求出n个小盆友最后做出决定后的最小冲突


题解:

  双倍经验,详情:BZOJ1934


参考代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
struct node
{
    int x,y,c,next,other;
}a[210000];int len,last[11000];
void ins(int x,int y,int c)
{
    int k1=++len,k2=++len;
    a[k1].x=x;a[k1].y=y;a[k1].c=c;
    a[k1].next=last[x];last[x]=k1;
    a[k2].x=y;a[k2].y=x;a[k2].c=0;
    a[k2].next=last[y];last[y]=k2;
    a[k1].other=k2;
    a[k2].other=k1;
}
int h[11000],list[11000],st,ed;
bool bt_h()
{
    memset(h,0,sizeof(h));
    h[st]=1;
    int head=1,tail=2;
    list[1]=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]==0&&a[k].c>0)
            {
                h[y]=h[x]+1;
                list[tail++]=y;
            }
        }
        head++;
    }
    if(h[ed]==0) return false;
    else return true;
}
int findflow(int x,int f)
{
    if(x==ed) return f;
    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&&f>s)
        {
            t=findflow(y,min(a[k].c,f-s));
            s+=t;
            a[k].c-=t;a[a[k].other].c+=t;
        }
    }
    if(s==0) h[x]=0;
    return s;
}
int c[310];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    st=0;ed=n+1;
    len=0;memset(last,0,sizeof(last));
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&c[i]);
        if(c[i]==1) ins(st,i,1);
        else ins(i,ed,1);
    }
    for(int i=1;i<=m;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        if(c[x]!=c[y])
        {
            if(c[x]==1) ins(x,y,1);
            else ins(y,x,1);
        }
    }
    int ans=0;
    while(bt_h())
    {
        ans+=findflow(st,999999999);
    }
    printf("%d
",ans);
    return 0;
}
原文地址:https://www.cnblogs.com/Never-mind/p/8321096.html