Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10384 Accepted: 3870
Description
Rain has pummeled the cows’ field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <= 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, don’t want to get their hooves dirty while they eat.
To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows’ field. Each of the boards is 1 unit wide, and can be any length long. Each board must be aligned parallel to one of the sides of the field.
Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. The boards may not cover any grass and deprive the cows of grazing area but they can overlap each other.
Compute the minimum number of boards FJ requires to cover all the mud in the field.
Input
Line 1: Two space-separated integers: R and C
Lines 2..R+1: Each line contains a string of C characters, with ‘*’ representing a muddy patch, and ‘.’ representing a grassy patch. No spaces are present.
OutputLine 1: A single integer representing the number of boards FJ needs.
Sample Input
4 4
..
.*
*.
..*.
Sample Output
4
Hint
OUTPUT DETAILS:
Boards 1, 2, 3 and 4 are placed as follows:
1.2.
.333
444.
..2.
Board 2 overlaps boards 3 and 4.
Source
USACO 2005 January Gold
题意:
给一个n*m的矩阵,其中为’*’的地方是需要用木板覆盖的,一个木板一次可以覆盖一行或一列,但不能将’.’一起覆盖。(即为’.’的地方就需要中断)
问最小需要多少个木板可以将这张图的所有’*’覆盖。
建图很巧。
经典的行列拆分。
先按行,把每一行连着的’*’看成一块。
再按列,把每一列连着的’*’看成一块。
如果行和列有交叉,就将这一行和这一列连一条边。
eg:对于样例:
..
.*
*.
..*.
按行:
1 0 2 0
0 3 3 3
4 4 4 0
0 0 5 0
即按行可以分成五个点。(覆盖五次)
按列:
6 0 9 0
0 8 9 10
7 8 9 0
0 0 9 0
把行放一边,列放一边,把每个’*’对应的行和列连边,要求最小需要多少木板覆盖,就可以转化为求二分图最小点覆盖,即二分图的最大匹配。
代码:
匈牙利:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int M = 100+10;
const int N = 400010;
const int INF = 1<<30;
int n,m;
char S[M][M];
inline int Min(int a,int b){
return a<b?a:b;
}
struct node{
int pre,v;
}edge[N];
int num=0;
int head[N];
void addedge(int from,int to){
num++;
edge[num].pre=head[from];
edge[num].v=to;
head[from]=num;
}
bool vis[N];
int totx=0,toty=0;
int mpx[M][M],mpy[M][M];
int c[N];
bool find(int u){
for(int i=head[u];i;i=edge[i].pre){
int v=edge[i].v;
if(!vis[v]){
vis[v]=true;
if(!c[v]||find(c[v])){
c[v]=u;
return true;
}
}
}
return false;
}
int maxxx(){
int ans=0;
for(int i=1;i<=totx;i++){
memset(vis,0,sizeof(vis));
if(find(i)) ans++;
}
return ans;
}
int main(){
memset(c,0,sizeof(c));
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",S[i]);
memset(mpx,0,sizeof(mpx));memset(mpy,0,sizeof(mpy));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int y=j;
if(S[i][y]=='*'){
totx++;
while(S[i][y]=='*'){
mpx[i][y]=totx;
y++;
}
}
j=y;
}
}
toty=totx;
for(int j=0;j<m;j++){
for(int i=0;i<n;i++){
int x=i;
if(S[x][j]=='*'){
toty++;
while(S[x][j]=='*'){
mpy[x][j]=toty;
x++;
}
}
i=x;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(S[i][j]=='*'){
addedge(mpx[i][j],mpy[i][j]);
}
}
}
printf("%d\n",maxxx());
return 0;
}
【今天顺便把匈牙利学了2333真的挺简单的】
dinic:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int M = 100+10;
const int N = 400010;
const int INF = 1<<30;
int n,m;
char S[M][M];
int s,t;
inline int Min(int a,int b){
return a<b?a:b;
}
struct node{
int pre,v,f;
}edge[N];
int num=1;
int head[N];
void addedge(int from,int to,int f){
num++;
edge[num].pre=head[from];
edge[num].v=to;
edge[num].f=f;
head[from]=num;
num++;
edge[num].pre=head[to];
edge[num].v=from;
edge[num].f=0;
head[to]=num;
}
int state[N],dis[N];
bool vis[N];
bool bfs(){
int h=0,tail=1;
state[1]=s,vis[s]=true,dis[s]=0;
do{
h++;
int u=state[h];
for(int i=head[u];i;i=edge[i].pre){
int v=edge[i].v;
if(!vis[v]&&edge[i].f){
tail++;
state[tail]=v;
vis[v]=true;
dis[v]=dis[u]+1;
}
}
}while(h<tail);
if(vis[t]) return true;
return false;
}
int dfs(int u,int delta){
if(u==t||delta==0) return delta;
int ans=0;
for(int i=head[u];i&δi=edge[i].pre){
int v=edge[i].v;
if(dis[v]==dis[u]+1&&edge[i].f){
int dd=dfs(v,Min(delta,edge[i].f));
edge[i].f-=dd;
edge[i^1].f+=dd;
ans+=dd;
delta-=dd;
}
}
if(!ans) dis[u]=-1;
return ans;
}
#define ms(x,y) memset(x,y,sizeof(x))
void zero(){
ms(dis,0);ms(vis,0);ms(state,0);
}
int maxflow(){
int ans=0;
while(1){
zero();
if(!bfs()) break;
ans+=dfs(s,INF);
}
return ans;
}
int totx=0,toty=0;
int mpx[M][M],mpy[M][M];
int main(){
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%s",S[i]);
memset(mpx,0,sizeof(mpx));memset(mpy,0,sizeof(mpy));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
int y=j;
if(S[i][y]=='*'){
totx++;
while(S[i][y]=='*'){
mpx[i][y]=totx;
y++;
}
}
j=y;
}
}
toty=totx;
for(int j=0;j<m;j++){
for(int i=0;i<n;i++){
int x=i;
if(S[x][j]=='*'){
toty++;
while(S[x][j]=='*'){
mpy[x][j]=toty;
x++;
}
}
i=x;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(S[i][j]=='*'){
addedge(mpx[i][j],mpy[i][j],1);
}
}
}
s=0,t=toty+1;
for(int i=1;i<=totx;i++) addedge(s,i,1);
for(int i=totx+1;i<=toty;i++) addedge(i,t,1);
printf("%d\n",maxflow());
return 0;
}