[hdu2119]二分图最小覆盖,最大匹配
题意:给一个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<int, int> 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]二分图最小覆盖,最大匹配的更多相关文章
- uva11419 二分图--最小覆盖=最大匹配
大白书355 // UVa11419 SAM I AM // Rujia Liu #include <cstdio> #include <cstring> #include & ...
- 【UVA11419 训练指南】我是SAM 【二分图最小覆盖,最小割】
题意 给出一个R*C大小的网格,网格上面放了一些目标.可以在网格外发射子弹,子弹会沿着垂直或者水平方向飞行,并且打掉飞行路径上的所有目标.你的任务是计算最少需要多少子弹,各从哪些位置发射,才能把所有目 ...
- 二分图最小覆盖的Konig定理及其证明,最小的覆盖证明
[转http://www.cppblog.com/abilitytao/archive/2009/09/02/95147.html -> http://yejingx.ycool.com/p ...
- hdu3729 I'm Telling the Truth (二分图的最大匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=3729 I'm Telling the Truth Time Limit: 2000/1000 MS (Java/ ...
- POJ 2584 T-Shirt Gumbo (二分图多重最大匹配)
题意 现在要将5种型号的衣服分发给n个参赛者,然后给出每个参赛者所需要的衣服的尺码的大小范围,在该尺码范围内的衣服该选手可以接受,再给出这5种型号衣服各自的数量,问是否存在一种分配方案使得每个选手都能 ...
- HDU 2444 The Accomodation of Students(二分图判定+最大匹配)
这是一个基础的二分图,题意比较好理解,给出n个人,其中有m对互不了解的人,先让我们判断能不能把这n对分成两部分,这就用到的二分图的判断方法了,二分图是没有由奇数条边构成环的图,这里用bfs染色法就可以 ...
- 二分图的最大匹配——最大流EK算法
序: 既然是个图,并且求边数的最大值.那么这就可以转化为网络流的求最大流问题. 只需要将源点与其中一子集的所有节点相连,汇点与另一子集的所有节点相连,将所有弧的流量限制置为1,那么最大流 == 最大匹 ...
- HDU 2389 Rain on your Parade / HUST 1164 4 Rain on your Parade(二分图的最大匹配)
HDU 2389 Rain on your Parade / HUST 1164 4 Rain on your Parade(二分图的最大匹配) Description You're giving a ...
- CF 277.5 B.BerSU Ball 二分图的最大匹配 模版题
题意:求二分图的最大匹配数量 模版如下: //二分图匹配(匈牙利算法的DFS实现) //初始化:g[][]两边顶点的划分情况 //建立g[i][j]表示i->j的有向边就可以了,是左边向右边的匹 ...
随机推荐
- web form常用控件
表单元素一共12个分三大类 文本类<input type="text" /> 文本框<input type="password& ...
- 2. Git-命令行-删除本地和远程分支
命令行方式 Git Bash: 切换到要操作的项目文件夹 命令行 : $ cd <ProjectPath> 查看项目的分支们(包括本地和远程) 命令行 : $ git branch -a ...
- billu b0x2靶机渗透
实战渗透靶机billu b0x2 攻击kali :192.168.41.147 靶机b0x2: 192.168.41.148 起手先nmap扫了一下 扫到了四个开放的端口,有ssh,http,rpcb ...
- TT企业微信社群辅助,企业微信社群辅助工具,允许批量添加好友,自动同意添加请求,自动回复消息
TT企业微信社群辅助,企业微信社群辅助工具,允许批量添加好友,自动同意添加请求,自动回复消息 界面截图 TT企业微信社群辅助工具下载 链接: https://pan.baidu.com/s/1Y2An ...
- ES6中对函数的扩展
ES6一路扩展,字符串.数组.数值.对象无一“幸免”,ES6说要雨露均沾,函数也不能落下,今天,就来讲解ES6对函数的扩展. 参数的默认值 在开发中,给函数的参数指定默认值,是很普遍很常见的一个需求, ...
- IIS WebDAV安全配置
本文为转载,原文地址:http://www.2cto.com/article/201307/228165.html IIS WebDAV安全配置 2013-07-16 12:13:00 作者:瞌睡龙收 ...
- swoole学习--图文直播和聊天室
其实这个也没有什么好值得记录的,但是前面都记下来了,我也顺便说说吧: 1.为了方便,最好把http服务声明为超全局变量. 2.在一些地方里面,你声明的http超全局变量是用不了的,你只能用他自己内置的 ...
- linux php 安装 openssl扩展
(1.生成 openssl.so 文件)#进入扩展目录cd /data/soft/php-5.5.38/ext/openssl#生成 configure 文件/usr/local/php/bin/ph ...
- Dockerfle创建镜像
简介 Dockerfile 由一行行命令语句组成,并且支持以 # 开头的注释行. 一般的,Dockerfile 分为四部分:基础镜像信息.维护者信息.镜像操作指令和容器启动时执行指令. # This ...
- VC++ QT 数组的初始化
数组有时会初始化为0. 但加了一个 QThread 的派生类对象之后,数组就不再被初始化为0了. 所以对于数组还是要手动初始化,否则可能产生无法预料的现象.