BZOJ 1934--善意的投票(最小割)

Posted Iscream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BZOJ 1934--善意的投票(最小割)相关的知识,希望对你有一定的参考价值。

    。。。

题目链接:

    http://www.lydsy.com/JudgeOnline/problem.php?id=1934 

Solution

    小朋友的冲突就是割,然后这道题要求的就是最小的割。。。

    从源点向刚开始为1的点连边,从每个刚开始为0的点向汇点连边,每条边容量为1。。。

    然后每一对好朋友之间加一条双向边,容量为1。。。

    都是套路。。。都是套路。。。

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define inf 1000000000
#define N 10000
using namespace std;
inline int Read(){
    int x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
char ch;
int n,m,S,T,ans;
int cnt=1;
int hed[N],h[N],q[N];
struct edge{
    int r,nxt,v;
}e[300000];
void insert(int u,int v,int w){
    e[++cnt].r=v;e[cnt].nxt=hed[u];hed[u]=cnt;e[cnt].v=w;
}
bool bfs(){
    int head=0,tail=1,now;
    memset(h,-1,sizeof(h));
    h[S]=1;q[1]=S;
    while(head!=tail){
        head++;now=q[head];
        for(int i=hed[now];i;i=e[i].nxt)
            if(e[i].v&&h[e[i].r]==-1){
                h[e[i].r]=h[now]+1;
                q[++tail]=e[i].r;
            }
    }
    return h[T]!=-1;
}
int dfs(int x,int F){
    if(x==T) return F;
    int w,used=0;
    for(int i=hed[x];i;i=e[i].nxt)
        if(h[x]+1==h[e[i].r]){
            w=F-used;
            w=dfs(e[i].r,min(w,e[i].v));
            e[i].v-=w;
            e[i^1].v+=w;
            used+=w;
            if(used==F) return F;
        }
    if(!used) h[x]=-1;
    return used;
}
void dinic(){
    while( bfs() )
        ans+=dfs(0,inf);
}
int main(){
	int sum=0;
	int u,v,w;
    n=Read();m=Read();
    S=0;T=n+1;
    for(int i=1;i<=n;i++){
    	w=Read();
    	if(w){
    		insert(S,i,1);
    		insert(i,S,0);
		}
    	else{
    		insert(i,T,1);
    		insert(T,i,0);
		}
	}
	for(int i=1;i<=m;i++){
		u=Read();v=Read();
		insert(u,v,1);
		insert(v,u,1);
	}
    dinic();
    printf("%d\n",ans);
    return 0;
}

  

  

This passage is made by Iscream-2001.

 

以上是关于BZOJ 1934--善意的投票(最小割)的主要内容,如果未能解决你的问题,请参考以下文章

BZOJ-1934: [Shoi2007]Vote 善意的投票 (网络流最小割)

BZOJ 1934 [Shoi2007]Vote 善意的投票(最小割)

BZOJ 1934 善意的投票(最小割)

bzoj 1934 : [Shoi2007]Vote 善意的投票 最小割

[bzoj1934]善意的投票

bzoj1934: [Shoi2007]Vote 善意的投票