1. 程式人生 > >BZOJ4808: 馬&BZOJ3175: [Tjoi2013]攻擊裝置

BZOJ4808: 馬&BZOJ3175: [Tjoi2013]攻擊裝置

data 大小 放置 continue http mes 題目 return names

BZOJ3175

Description

給定一個01矩陣,其中你可以在0的位置放置攻擊裝置。每一個攻擊裝置(x,y)都可以按照“日”字攻擊其周圍的 8個位置(x-1,y-2),(x-2,y-1),(x+1,y-2),(x+2,y-1),(x-1,y+2),(x-2,y+1), (x+1,y+2),(x+2,y+1)
求在裝置互不攻擊的情況下,最多可以放置多少個裝置。

Input

第一行一個整數N,表示矩陣大小為N*N。接下來N行每一行一個長度N的01串,表示矩陣。

Output

一個整數,表示在裝置互不攻擊的情況下最多可以放置多少個裝置。

Sample Input

3
010
000
100

Sample Output

4

HINT

100%數據 N<=200

題目傳送門

一眼最小割,黑白染色。。

至於為什麽。。。n<=200很可疑??

雙倍經驗真帶勁

代碼如下:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node{
    int x,y,c,next,other;
}a[
330000];int len,last[331000]; const int INF=1<<30; void ins(int x,int y,int c) { int k1,k2; k1=++len; a[len].x=x;a[len].y=y;a[len].c=c; a[len].next=last[x];last[x]=len; k2=++len; a[len].x=y;a[len].y=x;a[len].c=0; a[len].next=last[y];last[y]=len; a[k1].other
=k2; a[k2].other=k1; } int head,tail,st,ed; int h[41000],list[41000]; bool bt_h() { memset(h,0,sizeof(h));h[st]=1; list[1]=st,head=1,tail=2; 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 true; return false; } 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&&s<f) { s+=(t=findflow(y,min(a[k].c,f-s))); a[k].c-=t;a[a[k].other].c+=t; } } if(s==0)h[x]=0; return s; } const int dx[8]={1,1,-1,-1,2,2,-2,-2}; const int dy[8]={2,-2,2,-2,1,-1,1,-1}; int n; int p[210][210],cnt; char ss[210][210]; bool f[210][210]; int main() { scanf("%d",&n); len=0;memset(last,0,sizeof(last)); memset(f,true,sizeof(f)); cnt=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { p[i][j]=++cnt; if(j==1)f[i][j]^=f[i-1][j]; else f[i][j]^=f[i][j-1]; } int sum=0; for(int i=1;i<=n;i++) { scanf("%s",ss[i]+1); for(int j=1;j<=n;j++) if(ss[i][j]==0) sum++; } st=n*n+1,ed=st+1; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(ss[i][j]==1)continue; if(f[i][j]==0)ins(st,p[i][j],1); else ins(p[i][j],ed,1); } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(ss[i][j]==0&&f[i][j]==0) for(int k=0;k<8;k++) { int x=i+dx[k],y=j+dy[k]; if(1<=x&&x<=n&&1<=y&&y<=n&&ss[i][j]!=1) if(1==f[x][y]) ins(p[i][j],p[x][y],INF); } int ans=0; while(bt_h()==true){ans+=findflow(st,INF);} printf("%d\n",sum-ans); return 0; }

by_lmy

BZOJ4808: 馬&BZOJ3175: [Tjoi2013]攻擊裝置