UVaLive/LA 6811 Irrigation Lines(二部图,最小点覆盖)
2014-11-23 21:46
501 查看
6811 - Irrigation Lines |
矩形的农田,每行每列都有一个闸门,一些格子内中有庄稼,问最少开几个阀门,使得所有的庄稼都能得到灌溉。
解题思路:
明显的二部图最小点覆盖模型。基本属于二部图最大匹配的模板题。将行最为一个集合,列作为一个集合,庄稼(即行与列的交叉点)作为关系连边,问题转化为,同最少几个点(行或列),覆盖所有的边(庄稼),即最小点覆盖。
用匈牙利算法即可。
参考代码:
#include <iostream> #include <cstring> #include <cstdio> using namespace std; const int MAXN = 110; char graph[MAXN][MAXN]; bool visited[MAXN]; int nCase, cCase, use[MAXN], m, n; void init() { memset(use, -1, sizeof(use)); } void input() { scanf("%d%d", &m, &n); for (int i = 0; i < m; i++) { scanf("%s", graph[i]); } } bool find(int x) { for (int j = 0; j < n; j++) { if (graph[x][j] == '1' && !visited[j]) { visited[j] = true; if (use[j] == -1 || find(use[j])) { use[j] = x; return true; } } } return false; } int match() { int count = 0; for (int i = 0; i < m; i++) { memset(visited, false, sizeof(visited)); if (find(i)) count++; } return count; } void solve() { printf("Case #%d: %d\n", ++cCase, match()); } int main() { scanf("%d", &nCase); while (nCase--) { init(); input(); solve(); } return 0; }
相关文章推荐
- 二分图最小点覆盖 UVALive 6811
- UVALive 11419 SAM I AM (最小点覆盖输出)
- UVALive 3126 Taxi Cab Scheme(DAG的最小路径覆盖)
- UVAlive 3126(最小路径覆盖)
- Delivering Goods UVALive - 7986(最短路+最小路径覆盖)
- UVAlive 6571 最小路径覆盖变形(带权) 拆点+ISAP
- UVaLive 3126 Taxi Cab Scheme (最小路径覆盖)
- UVALive 2930 Minimizing Maximizer(最小区间覆盖数 DP + 线段树优化)
- uvalive3126最小路径覆盖
- UVALive 3126 出租车(DAG的最小不相交路径覆盖)
- UVAlive 7368 Airports(建图+最小路径覆盖)
- UVa live6492Welcome Party(二分最大匹配之最小点覆盖)
- UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)
- UVAlive3126 Taxi Cab Scheme(DAG的最小路径覆盖)
- [UVALive] 6492 Welcome Party(最小点覆盖)
- Taxi Cab Scheme UVALive - 3126 最小路径覆盖解法(必须是DAG,有向无环图) = 结点数-最大匹配
- UVALive-3126 Taxi Cab Scheme (DAG的最小路径覆盖)
- ACM->uvalive->Sentry Robots(二分图匹配的最小点覆盖)
- UVALive 4654 (最小路径覆盖)
- UVA -11419 SAM I AM 最小边覆盖输出路径