# bzoj1412-网络流最小割

2 2
2 2
1 1

2

10%的数据 n，m≤3
30%的数据 n，m≤20
100%的数据 n，m≤100

## Source

T的莫名其妙，明明是dinic模板

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pr;
const double pi=acos(-1);
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)
#define clr(a) memset(a,0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
#define pq priority_queue
#define pqb priority_queue <int, vector<int>, less<int> >
#define pqs priority_queue <int, vector<int>, greater<int> >
#define vec vector
#define T 10001
ld eps=1e-9;
ll pp=1000000007;
ll mo(ll a,ll pp){if(a>=0 && a<pp)return a;a%=pp;if(a<0)a+=pp;return a;}
ll powmod(ll a,ll b,ll pp){ll ans=1;for(;b;b>>=1,a=mo(a*a,pp))if(b&1)ans=mo(ans*a,pp);return ans;}
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
int dx[5]={0,-1,1,0,0},dy[5]={0,0,0,-1,1};
ll read(){ ll ans=0; char last=' ',ch=getchar();
while(ch<'0' || ch>'9')last=ch,ch=getchar();
while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
if(last=='-')ans=-ans; return ans;
}
#define M 500005
#define N 50005
const int INF=1<<30;
struct E_node{
int v,f,Next;
}edge[M];
int Map[1005][1005];

}
int bfs(int s,int t){
memset(level,0,sizeof(level));
level[s]=1;
int h=0,t_=1; q[h]=s;
while (h<t_){
int x=q[h++];
if (x==t) return 1;
int v=edge[i].v,f=edge[i].f;
if (!level[v] && f>0){
level[v]=level[x]+1;
q[t_++]=v;
}
}
}
return 0;
}
int dfs(int u,int maxf,int t){
if (u==t) return maxf;
int ret=0;
int v=edge[i].v,f=edge[i].f;
if (level[v]==level[u]+1 && f>0){
int Min=min(maxf-ret,f);
f=dfs(v,Min,t);
edge[i].f-=f;
edge[i^1].f+=f;
ret+=f;
if (ret==maxf) return ret;
}
}
return ret;
}
int Dinic(int s,int t){
int ans=0;
while (bfs(s,t)) ans+=dfs(s,INF,t);
return ans;
}
void build()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(Map[i][j]==1)
if(Map[i][j]==2)
for(int k=1;k<=4;k++)
{
int nowi=i+dx[k],nowj=j+dy[k];
if(nowi<1||nowi>n||nowj>m||nowj<1||Map[i][j]==2) continue;
if(Map[i][j]!=1||Map[nowi][nowj]!=1)
{
cout<<(i-1)*m+j<<' '<<(nowi-1)*m+nowj<<endl;
}
}

}
}
int main(){

for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&Map[i][j]);
build();
printf("%d\n",Dinic(0,T));
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#define inf 0x7fffffff
#define T 10001
using namespace std;
int cnt=1,ans,n,m;
int xx[4]={0,0,1,-1},yy[4]={1,-1,0,0},mp[105][105];
struct data{int to,next,v;}e[500001];
void ins(int u,int v,int w)
void insert(int u,int v,int w)
{ins(u,v,w);ins(v,u,0);}
bool bfs()
{
int t=0,w=1,i,now;
memset(h,-1,sizeof(h));
q[0]=0;h[0]=0;
while(t<w)
{
while(i)
{
if(e[i].v&&h[e[i].to]==-1)
{
h[e[i].to]=h[now]+1;
q[w++]=e[i].to;
}
i=e[i].next;
}
}
return h[T]==-1? 0:1;
}
int dfs(int x,int f)
{
if(x==T)return f;
int w,used=0,i;
while(i)
{
if(e[i].v&&h[e[i].to]==h[x]+1)
{
w=f-used;
w=dfs(e[i].to,min(w,e[i].v));
e[i].v-=w;
e[i^1].v+=w;
used+=w;
if(used==f)return f;
}
i=e[i].next;
}
if(!used)h[x]=-1;
return used;
}
void dinic(){while(bfs())ans+=dfs(0,inf);}
void ini()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&mp[i][j]);
}
void build()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
{
if(mp[i][j]==1)
insert(0,(i-1)*m+j,inf);
if(mp[i][j]==2)
insert((i-1)*m+j,T,inf);
for(int k=0;k<4;k++)
{
int nowi=i+xx[k],nowj=j+yy[k];
if(nowi<1||nowi>n||nowj>m||nowj<1||mp[i][j]==2) continue;
if(mp[i][j]!=1||mp[nowi][nowj]!=1)
{
cout<<(i-1)*m+j<<' '<<(nowi-1)*m+nowj<<endl;
insert((i-1)*m+j,(nowi-1)*m+nowj,1);
}
}

}
}
int main()
{
ini();
build();
dinic();
printf("%d",ans);
return 0;
}

posted @ 2017-08-08 21:54  dancer16  阅读(148)  评论(0编辑  收藏  举报