POJ 3041 Asteroids (minimum number of covers)

POJ3041

Meaning of the questions:

There is a N * N grid, the grid there are K obstructions. Do you have a weapon, every time you use the weapon can clear all the obstacles that particular row or column of the grid. You need at least ask how many times the weapon can clear all obstacles grid?

The minimum coverage problem, minimum coverage = maximum matching algorithm can be directly Hungary

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10010;
int head[N * 2], nex[N * 2], to[N * 2], match[N];
int cnt;
bool vis[N];
void pre_work() {
	memset(head, -1, sizeof head);
	memset(nex, -1, sizeof nex);
	memset(match, -1, sizeof match);
	cnt = 0;
	memset(vis, false, sizeof vis);
}
void add(int a, int b) {
	cnt++;
	nex[cnt] = head[a];
	head[a] = cnt;
	to[cnt] = b;
}
bool dfs(int p) {
	for (int i = head[p]; i != -1; i = nex[i]) {
		int y = to[i];
		if (!vis[y]) {
			vis[y] = true;
			if (match[y] == -1 || dfs(match[y])) {
				match[y] = p;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	ios::sync_with_stdio(0);
	int n, k;
	pre_work();
	cin >> n >> k;
	for (int i = 0; i < k; i++) {
		int a, b;
		cin >> a >> b;
		add(a, b + n);
		add(b + n, a);
	}
	int res = 0;
	for (int i = 1; i <= n; i++) {
		memset(vis, false, sizeof vis);
		if (dfs(i))++res;
	}
	cout << res << "\n";
	return 0;
}

 

Published 143 original articles · won praise 11 · views 8195

Guess you like

Origin blog.csdn.net/weixin_43701790/article/details/103758839