bzoj1735 [Usaco2005 jan]Muddy Fields 泥泞的牧场

传送门

分析

我们知道对于没有障碍的情况就是将横轴点于纵轴点连边

于是对于这种有障碍的情况我们还是分横轴纵轴考虑

只不过对于有障碍的一整条分为若干个无障碍小段来处理

然后将标号小段连边,跑最大匹配即可

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
int n,m,be1[110][110],be2[110][110],g[3000][3000],Ans,T,used[3000],belong[3000];
char s[110][110];
inline bool work(int x){
    for(int i=1;i<=m;i++)
      if(used[i]!=T&&g[x][i]){
          used[i]=T;
          if(!belong[i]||work(belong[i])){
            belong[i]=x;
            return 1;
        }
      }
    return 0;
}
inline void go(){
    for(int i=1;i<=n;i++){
      T=i;
      if(work(i))Ans++;
    }
}
int main(){
    int w,r,i,j,k;
    scanf("%d%d",&w,&r);
    for(i=0;i<w;i++)
      scanf("%s",s[i]);
    for(i=0;i<w;i++){
      k=1;
      for(j=0;j<r;j++){
          if(s[i][j]==‘.‘&&!k)k=1;
          if(s[i][j]==‘*‘){
            if(k)n++,k=0;
            be1[i][j]=n;
        }
      }
    }
    for(i=0;i<r;i++){
      k=1;
      for(j=0;j<w;j++){
          if(s[j][i]==‘.‘&&!k)k=1;
          if(s[j][i]==‘*‘){
            if(k)m++,k=0;
            be2[j][i]=m;
        }
      }
    }
    for(i=0;i<w;i++)
      for(j=0;j<r;j++)
        g[be1[i][j]][be2[i][j]]=1;
    go();
    cout<<Ans;
    return 0;
}

原文地址:https://www.cnblogs.com/yzxverygood/p/10336017.html

时间: 2024-08-01 09:01:48

bzoj1735 [Usaco2005 jan]Muddy Fields 泥泞的牧场的相关文章

BZOJ 1735: [Usaco2005 jan]Muddy Fields 泥泞的牧场

Description 大雨侵袭了奶牛们的牧场.牧场是一个R * C的矩形,其中1≤R,C≤50.大雨将没有长草的土地弄得泥泞不堪,可是小心的奶牛们不想在吃草的时候弄脏她们的蹄子.  为了防止她们的蹄子被弄脏,约翰决定在泥泞的牧场里放置一些木板.每一块木板的宽度为1个单位,长度任意.每一个板必须放置在平行于牧场的泥地里.    约翰想使用最少的木板覆盖所有的泥地.一个木板可以重叠在另一个木板上,但是不能放在草地上. Input 第1行:两个整数R和C. 第2到R+1行:每行C个字符,其中"*'代

bzoj 1689: [Usaco2005 Open] Muddy roads 泥泞的路

1689: [Usaco2005 Open] Muddy roads 泥泞的路 Description Farmer John has a problem: the dirt road from his farm to town has suffered in the recent rainstorms and now contains (1 <= N <= 10,000) mud pools. Farmer John has a collection of wooden planks of

bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路

bzoj1689[Usaco2005 Open] Muddy roads 泥泞的路 题意: 数轴上n个互不覆盖的区间,问要用多少个长为L的线段覆盖.n≤10000 题解: 排序区间,然后从每个区间左端点开始铺木板,如果最后一块木板能够铺到下一个区间就铺,以此类推. 代码: 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #define maxn 10100 5 #define inc(i

bzoj1689 / P1589 [Usaco2005 Open] Muddy roads 泥泞的路

P1589 [Usaco2005 Open] Muddy roads 泥泞的路 简单的模拟题. 给水坑排个序,蓝后贪心放板子. 注意边界细节. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struct data{int l,r;}a[10002]; 7 bool cmp(const data

poj 2226 Muddy Fields(合理建图+二分匹配)

1 /* 2 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 3 思路: 4 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1,然后让我们通过选择一行,或者 5 是一列将其所在行的或者所在列的 1全部删掉,求出最少需要几步? 6 7 这道题的思路就是:将行标 和 列标值为1的建立一条边!通过匈牙利算法可以得到这个二分图的最大匹配数 8 最大匹配数==最小顶点覆盖数!最小顶点覆盖就是用最少的点覆盖了这个二分图

BZOJ1689: [Usaco2005 Open] Muddy roads

1689: [Usaco2005 Open] Muddy roads Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 147  Solved: 107[Submit][Status][Discuss] Description Farmer John has a problem: the dirt road from his farm to town has suffered in the recent rainstorms and now contai

1677: [Usaco2005 Jan]Sumsets 求和

1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 626  Solved: 348[Submit][Status] Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers th

poj 2226 Muddy Fields(二分图最小点覆盖)

B - Muddy Fields Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2226 Description Rain has pummeled the cows' field, a rectangular grid of R rows and C columns (1 <= R <= 50, 1 <= C <

[BZOJ1677][Usaco2005 Jan]Sumsets 求和

1677: [Usaco2005 Jan]Sumsets 求和 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1031  Solved: 603 [Submit][Status][Discuss] Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use onl