POJ 2226 Muddy Fields(二分匹配【最大流】)

存档纪念
存档的原因是WA了很多次 然后试了很多样例终于试出来了
有很多要注意的地方 等以后详细说
参考样例在最后

#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;

//typedef pair<int, int> P;

/*
    二分图最大匹配最大流简化做法,复杂度O(|V||E|)
*/

const int inf = 2147483647;
const double eps = 1e-8;
const int maxn = 2500;
const int N = 55;
const ll mod = 1e9+7;

typedef pair<int,int> pa;
char map[N][N];
pa save[N][N];
int r, c;
int p, q;
vector<int> G[maxn];
int match[maxn];        //匹配 
bool used[maxn];

void addedge(int u, int v){
    G[u].push_back(v);
    G[v].push_back(u);
}

//dfs寻找增广路 
bool dfs(int v){
    used[v] = true;
    for(int i=0; i<G[v].size(); i++){
        int u = G[v][i], w = match[u];
        if(w<0 || !used[w] && dfs(w)){  //u未匹配过,或还有增广路 
            match[v] = u;
            match[u] = v;
            return true;
        }
    }
    return false;
}

//求解二分图的最大匹配 
int bipartite_matching(){
    int res = 0;
    memset(match, -1, sizeof(match));
    for(int v = 0; v< p + q; v++){
        if(match[v] < 0){
            memset(used, 0, sizeof(used));
            if(dfs(v)){
                res++;
            }
        }
    } 
    return res;
}

int main(){
    while(~scanf("%d %d", &r, &c)){
        for(int i=0; i<maxn; i++){
            G[i].clear();
            used[i] = 0;

        }
        memset(save, -1, sizeof(save));
        memset(map, 0, sizeof(map));
        for(int i = 0; i < r; i++){
            scanf("%s",map[i]);
        } 
        p = 0;
        int flag = 0;
        for(int i = 0; i < c; i++){
            flag = 0;
            for(int j = 0; j < r; j++){
                if(map[j][i] == '*'){
                    save[j][i].first = p;
                    flag = 1;
                }
                else{
                    save[j][i].first = -1;
                    if(flag) p++;
                    flag = 0;

                }
            }
            if(flag) p++;
        }

        q = 0;
        flag = 0;
        for(int i = 0; i < r; i++){
            flag = 0;
            for(int j = 0; j < c; j++){
                if(map[i][j] == '*'){
                    save[i][j].second = q;
                    flag = 1;
                }
                else{
                    save[i][j].second = -1;
                    if(flag) q++;
                    flag = 0;
                }
            }
            if(flag) q++;
        }

//      
//      for(int i = 0; i < r; i++){
//          for(int j = 0; j < c; j++){
//              cout<<save[i][j].first<<","<<save[i][j].second<<"  ";
//          }
//          puts("");
//      }
//      
        for(int i = 0; i < r; i++){
            for(int j = 0; j < c; j++){
                if(save[i][j].first!=-1&&save[i][j].second!=-1){
//                  system("pause");
//                  cout<<"i: "<<i<<" j: "<<j<<endl;
//                  cout<<save[i][j].first<<" "<<save[i][j].second<<endl;
                    addedge(save[i][j].first,p + save[i][j].second );
                }
            }   
        }

//      int ans = bipartite_matching();


//      for(int i=0; i<n; i++){
//          int m;
//          scanf("%d", &m);
//          for(int j=0; j<m; j++){
//              int b;
//              scanf("%d", &b);
//              addedge(i, n+b-1);
//          }
//      }
        printf("%d\n", bipartite_matching());
    }
    return 0;
} 
/*
3 4
.***
.***
.***
5 5
.*.*.
*.*.*
.*.*.
*.*.*
.*.*.
16 16
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
.***.***.***.***
50 50
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
.***.***.***.***.***.***.***.***.***.***.***.*****
50 50
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.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*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*
*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.
*/

小数据和大数据的样例是必要的!
一定要注意数组长度&用模板时总点数的范围

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值