码迷,mamicode.com
首页 > 其他好文 > 详细

[hdu2119]二分图最小覆盖,最大匹配

时间:2015-08-02 06:19:49      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

题意:给一个01矩阵,每次可以选一行或一列,打掉上面所有的1,求打掉所有的1所需的最小次数。

思路:经典的模型了,二分图最小覆盖=最大匹配。所谓最小覆盖是指选最少的点关联所有的边。容易得到将行和列看成点,1看成边,那么就是选尽量少的行和列来关联所有的1,最小覆盖模型,用最大匹配做。可以选择匈牙利算法,或者直接最大流。


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* ******************************************************************************** */
#include <iostream>                                                                 //
#include <cstdio>                                                                   //
#include <cmath>                                                                    //
#include <cstdlib>                                                                  //
#include <cstring>                                                                  //
#include <vector>                                                                   //
#include <ctime>                                                                    //
#include <deque>                                                                    //
#include <queue>                                                                    //
#include <algorithm>                                                                //
#include <map>                                                                      //
#include <cmath>                                                                    //
using namespace std;                                                                //
                                                                                    //
#define pb push_back                                                                //
#define mp make_pair                                                                //
#define X first                                                                     //
#define Y second                                                                    //
#define all(a) (a).begin(), (a).end()                                               //
#define fillchar(a, x) memset(a, x, sizeof(a))                                      //
                                                                                    //
typedef pair<intint> pii;                                                         //
typedef long long ll;                                                               //
typedef unsigned long long ull;                                                     //
                                                                                    //
#ifndef ONLINE_JUDGE                                                                //
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}    //
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>                    //
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;          //
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>      //
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>              //
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>   //
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}   //
#endif // ONLINE_JUDGE                                                              //
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}        //
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}        //
template<typename T>                                                                //
void V2A(T a[],const vector<T>&b){for(int i=0;i<b.size();i++)a[i]=b[i];}            //
template<typename T>                                                                //
void A2V(vector<T>&a,const T b[]){for(int i=0;i<a.size();i++)a[i]=b[i];}            //
                                                                                    //
const double PI = acos(-1.0);                                                       //
const int INF = 1e9 + 7;                                                            //
                                                                                    //
/* -------------------------------------------------------------------------------- */
 
struct Edmonds {
    const static int maxn = 1e2 + 7;
    int n, m;
    bool g[maxn][maxn];
    bool vis[maxn];
    int left[maxn];
 
    void init(int n, int m) {
        this->n = n;
        this->m = m;
        memset(g, 0, sizeof(g));
        memset(left, -1, sizeof(left));
    }
    void add(int u, int v) {
        g[u][v] = true;
    }
    bool match(int u) {
        for(int v = 1; v <= m; v++)if(g[u][v] && !vis[v]) {
                vis[v] = true;
                if(left[v] == -1 || match(left[v])) {
                    left[v] = u;
                    return true;
                }
            }
        return false;
    }
 
    int solve() {
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            memset(vis, 0, sizeof(vis));
            if(match(i)) ans++;
        }
        return ans;
    }
};/** 点从1开始编号 **/
Edmonds solver;
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt""r", stdin);
#endif // ONLINE_JUDGE
    int n, m;
    while (cin >> n, n) {
        cin >> m;
        solver.init(n, m);
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < m; j ++) {
                int x;
                scanf("%d", &x);
                if (x) solver.add(i + 1, j + 1);
            }
        }
        cout << solver.solve() << endl;
    }
    return 0;
}
/* ******************************************************************************** */

[hdu2119]二分图最小覆盖,最大匹配

标签:

原文地址:http://www.cnblogs.com/jklongint/p/4695002.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!