题目
Source
http://www.lightoj.com/volume_showproblem.php?problem=1171
Description
Given an m x n chessboard where some of the cells are broken. Now you are about to place chess knights in the chessboard. You have to find the maximum number of knights that can be placed in the chessboard such that no two knights attack each other. You can't place knights in the broken cells.
Those who are not familiar with chess knights, note that a chess knight can attack eight positions in the board as shown in the picture below.
Input
Input starts with an integer T (≤ 125), denoting the number of test cases.
Each case starts with a blank line. The next line contains three integers m, n, K (1 ≤ m, n ≤ 200). Here m and n corresponds to the number of rows and the number of columns of the board respectively. Each of the next K lines will contain two integers x, y (1 ≤ x ≤ m, 1 ≤ y ≤ n) denoting that the cell(x, y) is broken already. No broken cell will be reported more than once.
Output
For each case of input, print the case number and the maximum number of knights that can be placed in the board considering the above restrictions.
Sample Input
2
8 8 0
2 5 4
1 3
1 4
2 3
2 4
Sample Output
Case 1: 32
Case 2: 6
分析
题目大概说一个n*m的国际象棋棋盘上有些格子不能放棋子,问最多能放几个骑士使得它们都不会处于互相攻击的状态。
棋盘黑白染色,形成二分图,然后就是二分图最大点独立集模型了,结果即为所有点数-二分图最大匹配。
代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN 44444
#define MAXM 44444*22 struct Edge{
int v,cap,flow,next;
}edge[MAXM];
int vs,vt,NE,NV;
int head[MAXN]; void addEdge(int u,int v,int cap){
edge[NE].v=v; edge[NE].cap=cap; edge[NE].flow=0;
edge[NE].next=head[u]; head[u]=NE++;
edge[NE].v=u; edge[NE].cap=0; edge[NE].flow=0;
edge[NE].next=head[v]; head[v]=NE++;
} int level[MAXN];
int gap[MAXN];
void bfs(){
memset(level,-1,sizeof(level));
memset(gap,0,sizeof(gap));
level[vt]=0;
gap[level[vt]]++;
queue<int> que;
que.push(vt);
while(!que.empty()){
int u=que.front(); que.pop();
for(int i=head[u]; i!=-1; i=edge[i].next){
int v=edge[i].v;
if(level[v]!=-1) continue;
level[v]=level[u]+1;
gap[level[v]]++;
que.push(v);
}
}
} int pre[MAXN];
int cur[MAXN];
int ISAP(){
bfs();
memset(pre,-1,sizeof(pre));
memcpy(cur,head,sizeof(head));
int u=pre[vs]=vs,flow=0,aug=INF;
gap[0]=NV;
while(level[vs]<NV){
bool flag=false;
for(int &i=cur[u]; i!=-1; i=edge[i].next){
int v=edge[i].v;
if(edge[i].cap!=edge[i].flow && level[u]==level[v]+1){
flag=true;
pre[v]=u;
u=v;
//aug=(aug==-1?edge[i].cap:min(aug,edge[i].cap));
aug=min(aug,edge[i].cap-edge[i].flow);
if(v==vt){
flow+=aug;
for(u=pre[v]; v!=vs; v=u,u=pre[u]){
edge[cur[u]].flow+=aug;
edge[cur[u]^1].flow-=aug;
}
//aug=-1;
aug=INF;
}
break;
}
}
if(flag) continue;
int minlevel=NV;
for(int i=head[u]; i!=-1; i=edge[i].next){
int v=edge[i].v;
if(edge[i].cap!=edge[i].flow && level[v]<minlevel){
minlevel=level[v];
cur[u]=i;
}
}
if(--gap[level[u]]==0) break;
level[u]=minlevel+1;
gap[level[u]]++;
u=pre[u];
}
return flow;
} bool map[222][222];
int dx[]={1,1,-1,-1,2,2,-2,-2};
int dy[]={2,-2,2,-2,1,-1,1,-1};
int main(){
int t,n,m,k;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
scanf("%d%d%d",&n,&m,&k);
memset(map,0,sizeof(map));
int a,b,tot=n*m;
while(k--){
scanf("%d%d",&a,&b);
--a; --b;
map[a][b]=1;
}
for(int i=0; i<n; ++i){
for(int j=0; j<m; ++j){
if(map[i][j]) --tot;
}
}
vs=n*m; vt=vs+1; NV=vt+1; NE=0;
memset(head,-1,sizeof(head));
for(int i=0; i<n*m; ++i){
int x=i/m,y=i%m;
if(map[x][y]) continue;
if(x+y&1) addEdge(i,vt,1);
else{
addEdge(vs,i,1);
for(int j=0; j<8; ++j){
int nx=x+dx[j],ny=y+dy[j];
if(nx<0 || nx>=n || ny<0 || ny>=m || map[nx][ny]) continue;
addEdge(i,nx*m+ny,1);
}
}
}
printf("Case %d: %d\n",cse,tot-ISAP());
}
return 0;
}