# [BZOJ 2127]happiness（最小割）

Description

Solution

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int n,m,s,t,ans=0;
struct Node
{
int next,to,cap;
}Edges[500005];
{
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;c=getchar();
}
while(c>='0'&&c<='9'){
x=x*10+c-'0';c=getchar();
}
return x*f;
}
{
Edges[cnt].to=v;
Edges[cnt++].cap=c;
}
void insert(int u,int v,int c)
{
}
int pos(int x,int y){return (x-1)*m+y;}
queue<int>q;
bool bfs()
{
memset(level,-1,sizeof(level));
q.push(s);level[s]=0;
while(!q.empty())
{
int u=q.front();q.pop();
{
int v=Edges[i].to;
if(level[v]==-1&&Edges[i].cap)
level[v]=level[u]+1,q.push(v);
}
}
if(level[t]==-1)return false;
return true;
}
int dfs(int u,int f)
{
if(u==t)return f;
int flow=0,d;
{
int v=Edges[i].to;
if(level[v]==level[u]+1&&Edges[i].cap)
{
d=dfs(v,min(f-flow,Edges[i].cap));
flow+=d;
Edges[i].cap-=d;
Edges[i^1].cap+=d;
}
}
if(!flow)level[u]=-1;
return flow;
}
int main()
{
s=0,t=n*m+1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
insert(s,pos(i,j),x);
ans+=x;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
insert(pos(i,j),t,x);
ans+=x;
}
int num=t+1;
for(int i=1;i<=n-1;i++)
for(int j=1;j<=m;j++)
{
insert(s,num,x);
insert(num,pos(i,j),INF);
insert(num,pos(i+1,j),INF);
ans+=x;
num++;
}
for(int i=1;i<=n-1;i++)
for(int j=1;j<=m;j++)
{
insert(num,t,x);
insert(pos(i,j),num,INF);
insert(pos(i+1,j),num,INF);
ans+=x;
num++;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m-1;j++)
{
insert(s,num,x);
insert(num,pos(i,j),INF);
insert(num,pos(i,j+1),INF);
ans+=x;
num++;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m-1;j++)
{
insert(num,t,x);
insert(pos(i,j),num,INF);
insert(pos(i,j+1),num,INF);
ans+=x;
num++;
}
int d;
while(bfs())
{
while(d=dfs(s,INF))
ans-=d;
}
printf("%d\n",ans);
return 0;
} 

posted @ 2017-05-17 21:36  Zars19  阅读(191)  评论(0编辑  收藏  举报