(有任何问题欢迎留言或私聊 && 欢迎交流讨论哦
目录
题意:传送门
原题目描述在最下面。
一个nm的矩阵,有坑有草,可以用1x长度的木板盖住坑,但不能盖到草。问最少用多少木板可以盖掉坑。
思路:
二分图最小路径覆盖 = N - 最大匹配
- 把所有同一横排的坑统一标号,同一竖排的坑统一标号。
- 对于每个坑,有横编号向竖编号连边,然后跑匈牙利算法,当然你也可以跑网络流。
####AC代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<cctype>
#include<string>
#include<cmath>
#define mme(a,b) memset((a),(b),sizeof((a)))
#define fuck(x) cout<<"* "<<x<<"
"
#define all(x) (x).begin(),(x).end()
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL xiao = 0xc0c0c0c0c0c0c0c0;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const int N = 2500+5;
int ab(int x){return x<0?-x:x;}
int n, m;
struct lp{
int v,nex;
}cw[N*N];
int head[N],tot,vis[N],be[N];
char ar[N][N];
int boy[N][N],gril[N][N];
void init(){
mme(head,-1);mme(be,-1);
tot=-1;mme(boy,0);mme(gril,0);
}
void add(int u,int v){
cw[++tot].v=v;cw[tot].nex=head[u];
head[u]=tot;
}
bool dfs(int u){
for(int i=head[u];~i;i=cw[i].nex){
int v = cw[i].v;
if(vis[v])continue;
vis[v]=1;
if(be[v]==-1||dfs(be[v])){
be[v]=u;
return true;
}
}
return false;
}
void hungry(){
int ans=0;
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j)vis[j]=0;
if(dfs(i))ans++;
}
printf("%d
", (ans));
}
int main(){
#ifndef ONLINE_JUDGE
freopen("E://ADpan//in.in", "r", stdin);
//freopen("E://ADpan//out.out", "w", stdout);
#endif
while(~scanf("%d%d",&n,&m)){
init();
int n1=0,n2=0;
for(int i=0;i<n;++i){
scanf("%s",ar[i]);
for(int j=0;j<m;++j){
if(ar[i][j]=='*'){
if(j>=1&&ar[i][j-1]=='*'){
boy[i][j] = boy[i][j-1];
}else boy[i][j] = ++n1;
}
}
}
for(int i=0;i<n;++i){
for(int j=0;j<m;++j){
if(ar[i][j]=='*'){
if(i>=1&&ar[i-1][j]=='*'){
gril[i][j] = gril[i-1][j];
}else gril[i][j] = ++n2;
add(boy[i][j],gril[i][j]+n1);
}
}
}
n = n1+n2;
hungry();
}
return 0;
}
最近突然产生了二分图是建单向边还是双向边的疑惑,因为看到这个题建双向边也有过的。仔细看,原来建图和标号是不一样的。有人是把横向编号和竖向标号当成一个部分,有人是当成两个部分。当为两个部分就可以建双向边了,但是似乎没有必要,因为单向边一样可以过,另一部分是没有实际作用的,我在dfs的过程中根本不会搜索到另一部分的点,搜索的全是一边的,如果`u,be[v]`这样都是一边的。
什么时候要建双向边的呢?看题目要求吧,像那些两边点一样的时候,可能要建双向边,比如,似乎hdu2444要双向边才行。
原题目描述:
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.
Output
- Line 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