标签:i++ his += class tail ace str 代码 最大流
比较好想的最小割。。。
题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=1412
其实就是把狼和羊隔开。。。
从源点向每只狼连边,从每只羊向汇点连边,容量为无限大。。。
然后每只狼向周围空格和羊连边,每个空格向周围空格和羊连边,容量都为1.。。。
最后跑一遍最大流。。。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define inf 1000000000
#define N 1000000
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,d,S,T,ans,tot;
int cnt=1;
int hed[N],h[N],q[N];
int xx[5]={0,-1,0,1,0},yy[5]={0,0,1,0,-1};
int mp[110][110];
struct edge{
int r,nxt,v;
}e[500000];
void insert(int u,int v,int w){
e[++cnt].r=v;e[cnt].nxt=hed[u];hed[u]=cnt;e[cnt].v=w;
e[++cnt].r=u;e[cnt].nxt=hed[v];hed[v]=cnt;e[cnt].v=0;
}
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*m+1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
mp[i][j]=Read();
if(mp[i][j]==1)insert(S,(i-1)*m+j,inf);
if(mp[i][j]==2)insert((i-1)*m+j,T,inf);
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
if(mp[i][j]==2)continue;
for(int k=1;k<=4;k++){
if(i+xx[k]<=n && i+xx[k]>=1 && j+yy[k]<=m && j+yy[k]>=1)
if(mp[i+xx[k]][j+yy[k]]==0 || mp[i+xx[k]][j+yy[k]]==2)
insert((i-1)*m+j,(i+xx[k]-1)*m+j+yy[k],1);
}
}
dinic();
printf("%d\n",ans);
return 0;
}
This passage is made by Iscream-2001.
标签:i++ his += class tail ace str 代码 最大流
原文地址:http://www.cnblogs.com/Yuigahama/p/7746130.html