【最小点覆盖】Asteroids

L i n k Link

S S L   1341 SSL\ 1341

D e s c r i p t i o n Description

题目给出一个矩阵,上面有敌人,每个子弹可以打出一横行或者一竖行,问最少用多少子弹消灭都有敌人,如:
X.X
.X.
.X.
x表示敌人,显然用两个子弹就可以解决所有敌人。

S a m p l e Sample I n p u t Input

3 4(点数n和边数k)
1 1(a连向b)
1 3
2 2
3 2

S a m p l e Sample O u t p u t Output

2

H i n t Hint

1 <= N <= 500
1 <= K <= 10,000

T r a i n Train o f of T h o u g h t Thought

将题目转化为二分图,会发现就是要求最少要在多少个点开枪,才能使得所有边都被打到,其实就是求最小点覆盖
又∵最小点覆盖 = 最大匹配
∴直接求一个最大匹配就好了

C o d e Code

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;
int q, n, m, s, ans, t;
int c[205], h[205], link[205];

struct node
{
	int to, next;
}g[20005];

void add(int a, int b)
{g[++t] = (node){b, h[a]}; h[a] = t;}

bool find(int x)
{
	for (int i = h[x]; i; i = g[i].next)
	{
		if (!c[g[i].to])
		{
			c[g[i].to] = 1;
			q = link[g[i].to];
			link[g[i].to] = x;
			if (q == 0 || find(q)) return 1;
			link[g[i].to] = q;
		}
	}
	return 0;
}

int main()
{
	scanf("%d%d", &n, &m); 
	for (int i = 1; i <= m; ++i)
	{
		int a, b;
		scanf("%d%d", &a, &b);
		add(a, b);             
	}
	for (int i = 1; i <= n; ++i) 
	{
		memset(c, 0, sizeof(c));
		if (find(i)) ans++;
	}
	printf("%d", ans);
}

猜你喜欢

转载自blog.csdn.net/LTH060226/article/details/104016462
今日推荐