您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息