Asteroids POJ - 3041 匈牙利算法+最小点覆盖König定理

题意: 给出一个N*N的地图N   地图里面有K个障碍     你每次可以选择一条直线 消除这条直线上的所有障碍  (直线只能和列和行平行)

问最少要消除几次

题解: 如果(x,y)上有一个障碍 则把X加入点集 V1 、Y加入点集V2   并且X Y连一条边  这样构成一个新图

如果选择 V1中的点 X 那么就相当于消去 (X,y)中的所有Y    要找使最小点覆盖 那就是跑一遍 匈牙利就行了 详细证明见二分图最小点覆盖K?nig定理

其中 x y需要连单向边 不然会造成混乱 因为x=1 y=1是不同的含义

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 const int maxn=1000;
 6 int n,k;
 7 int mp[maxn][maxn];
 8 int girl[maxn];
 9 int used[maxn];
10 bool find(int x){
11     int i,j;
12     for(j=1;j<=n;j++){
13         if(mp[x][j]==1&&used[j]==0){
14             used[j]=1;
15             if(girl[j]==0||find(girl[j])){
16                 girl[j]=x;
17                 return 1;
18             }
19         }
20     }
21     return 0;
22 }
23 int main(){
24     cin>>n>>k;
25     for(int i=1;i<=k;i++){
26         int a,b;
27         scanf("%d%d",&a,&b);//建边
28         mp[a][b]=1;
29     }
30     int sum=0;
31     for(int i=1;i<=n;i++){
32         memset(used,0,sizeof(used));//每次要初始化 因为used是用于判断之前有没有用过 什么叫之前有没用用过  比如进入3的递归,used[2]=1,3 和2匹配  2已经在之前已经有归属了  现在要进行拆边 find(girl(j))  此时假设girl(j)=5,5就不能和2匹配了,因为现在进行拆边操作时逻辑上已经认定 上层递归的used[2]=1 已经用了2
33         if(find(i))sum++;
34     }
35 cout<<sum<<endl;
36     return 0;
37 }

原文地址:https://www.cnblogs.com/ttttttttrx/p/10012352.html

时间: 2024-08-27 10:36:48

Asteroids POJ - 3041 匈牙利算法+最小点覆盖König定理的相关文章

POJ 3041 Asteroids(二分图 &amp;&amp; 匈牙利算法 &amp;&amp; 最小点覆盖)

嗯... 题目链接:http://poj.org/problem?id=3041 这道题的思想比较奇特: 把x坐标.y坐标分别看成是二分图两边的点,如果(x,y)上有行星,则将(x,y)之间连一条边,而我们要做的就是要找尽量少的点把所有的边覆盖,即为最小点覆盖问题,根据König定理:最小覆盖点数=最大匹配数,所以就可以用匈牙利算法求最大匹配了. AC代码: 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream&

POJ 3041 匈牙利算法模板题

一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分别是行数和列数 每有一个点 就让所处行列连一条边 求最小点覆盖 然后卡住...后来看了增林的博客... 最小点覆盖=最大匹配数 果然是模板题.. 然后wa.. 后来发现是当进行对左边点的遍历的时候 每次都要mem一次vis数组 应该是每次找之前都重新清空啊..不然下次怎么找啊...增光路对点的是否被

Asteroids POJ - 3041 二分图最小点覆盖

Asteroids POJ - 3041 Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice point

POJ 1463 Strategic game 最小点覆盖集(树形dp)

点击打开链接 Strategic game Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 6105   Accepted: 2808 Description Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is v

poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利用这个武器摧毁所有的小行星最少需要几发光束. 主要是构图,将每一行当成一个点,构成集合1,每一列也当成一个点,构成集合2,每一个障碍物的位置坐标将集合1和集合2的点连接起来,也就是将每一个障碍物作为连接节点的边,这样可以得出本题是一个最小点覆盖的问题==二分图的最大匹配. 就可以通过匈牙利算法求解.

Asteroids POJ - 3041 【最小点覆盖集】

Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. Fortun

poj 1325 Machine Schedule 最小点覆盖

题目链接:http://poj.org/problem?id=1325 As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that must be satis

hdoj 1150 Machine Schedule【匈牙利算法+最小顶点覆盖】

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6701    Accepted Submission(s): 3358 Problem Description As we all know, machine scheduling is a very classical problem in comput

Asteroids - poj 3041(二分图最大匹配问题)

Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17258   Accepted: 9386 Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1