pku3041 Asteroids

大意:N*N的方阵中有m个怪兽,每开一枪可以杀死一行或者一列中的所有怪兽。至少要开多少强。
算法:将行列看作点,怪兽看作边。即现在需要选择最小的点数来关联所有的边。即求最小点覆盖。即最大匹配。
 
 1 #include<iostream>
2 #define SIZE 501
3 using namespace std;
4 int n,k,x,y;
5 int t[SIZE];
6 bool v[SIZE],str[SIZE][SIZE];
7 bool find(int x)
8 {
9 for(int i=0;i<n;i++)
10 {
11 if(v[i]==false&&str[x][i]==true) //i未匹配,x到i有边
12 {
13 v[i]=true; //选择x到i的边
14 if(t[i]==-1||find(t[i]))
15 {
16 t[i]=x;
17 return true;
18 }
19 }
20 }
21 return false;
22 }
23 int mach(void)
24 {
25 int count=0;
26 for(int i=0;i<n;i++)
27 {
28 memset(v,false,sizeof(v));
29 if(find(i)) count++;
30 }
31 return count;
32 }
33 int main()
34 {
35 while(scanf("%d %d",&n,&k)!=EOF)
36 {
37 memset(t,-1,sizeof(t));
38 memset(str,false,sizeof(str));
39 while(k--)
40 {
41 scanf("%d %d",&x,&y);
42 str[x-1][y-1]=true;
43 }
44 printf("%d\n",mach());
45 }
46 return 0;
47 }
原文地址:https://www.cnblogs.com/tiankonguse/p/2403075.html