Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25232   Accepted: 13625

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 <= K <= 10,000), which are conveniently located at the lattice points of the grid. 

Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.

Input

* Line 1: Two integers N and K, separated by a single space. 
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.

Output

* Line 1: The integer representing the minimum number of times Bessie must shoot.

Sample Input

3 4
1 1
1 3
2 2
3 2

Sample Output

2

Hint

INPUT DETAILS: 
The following diagram represents the data, where "X" is an asteroid and "." is empty space: 
X.X 
.X. 
.X.
 

OUTPUT DETAILS: 

Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).

题目传送门:点击打开链接

题目大意:给你一个n*n的矩阵,和m个行星的坐标,你有一种武器,每一次使用可以把一行或者一列的行星清空,现在要你使用最少的次数,将行星清空,输出最少次数。

思路:因为这道题的分类是匈牙利算法,最大匹配数,所以我知道不是贪心,(其实想一下,贪心是错的)。但是头秃没思路啊,后来知道了匈牙利算法的一个重要结论,最小覆盖点集=最大匹配数,什么是最小覆盖点集呢?就是能把一副二分图所有的边都有一端点存在于这个集合中(就是把每条边都碰到了),这个点可以出现在二分图的任意一边,数量等于最大匹配数,这个结论我是现学现卖的所以大家可以自己百度证明。(与此相像的还有一个定理,是最小覆盖边集=顶点数-最大匹配数。)

利用这个结论,我们可以想,我们是要选择行和列,把所有的行星都炸掉,和这个结论中利用点,覆盖所有的边思路是一样的,所以这道题的二分图点集就是,左边是行,右边是列,然后用坐标来表示连接关系,比如存在(2,3)这个点,那就是第二行和第三列建边。最后匈牙利算法求一下就可以了。

这里再推荐一个匈牙利算法讲的很好的博客:点击打开链接

最后上代码,都不需要注释啦嘿嘿。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<iostream>
#include<algorithm>
#define MAXN 10100
#define INF 0x7fffffff
#define max(a,b) a>b?a:b
#define min(a,b) a>b?b:a
using namespace std;
const int maxn=600;
int g[maxn][maxn];
int girl[maxn],used[maxn];
int n,m;
bool find(int x){
for(int i=1;i<=n;i++){
if(g[x][i]&&used[i]==0){
used[i]=1;
if(girl[i]==0||find(girl[i])){
girl[i]=x;
return true;
}
}
}
return false;
}
int main(){
cin>>n>>m;
while(m--){
int c,r;
scanf("%d%d",&c,&r);
g[c][r]=1;
}
int ans=0;
for(int i=1;i<=n;i++){
memset(used,0,sizeof(used));
if(find(i))ans++;
}
printf("%d\n",ans);
}

POJ3041轰炸行星(匈牙利算法 最小覆盖点集)的更多相关文章

  1. poj3041 Asteroids 匈牙利算法 最小点集覆盖问题=二分图最大匹配

    /** 题目:poj3041 Asteroids 链接:http://poj.org/problem?id=3041 题意:给定n*n的矩阵,'X'表示障碍物,'.'表示空格;你有一把枪,每一发子弹可 ...

  2. poj3020 建信号塔(匈牙利算法 最小覆盖边集)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10518   Accepted: 518 ...

  3. POJ3041 Asteroids(匈牙利算法)

    嘟嘟嘟 虽然我已经会网络流了,但是还是学了一个匈牙利算法. --就跟我会线段树,但还是学了树状数组一样. 其实匈牙利算法挺暴力的.简单来说就是先贪心匹配,然后如果左部点\(i\)匹配不上了,就尝试更改 ...

  4. Machine Schedule(二分图匹配之最小覆盖点,匈牙利算法)

    个人心得:二分图啥的一点都不知道,上网借鉴了下,请参考http://blog.csdn.net/thundermrbird/article/details/52231639 加上自己的了解,二分图就是 ...

  5. HDU 1150 Machine Schedule (最小覆盖,匈牙利算法)

    题意: 有两台不同机器A和B,他们分别拥有各种运行模式1~n和1~m.现有一些job,需要在某模式下才能完成,job1在A和B上需要的工作模式又可能会不一样.两台机器一开始处于0模式,可以切换模式,但 ...

  6. UVA 11419 SAM I AM (最小点覆盖,匈牙利算法)

    题意:给一个r*c的矩阵,某些格子中可能有一些怪物,可以在一行或一列防止一枚大炮,大炮会扫光整行/列的怪,问最少需要多少炮?输出炮的位置. 思路: 先每行和列都放一个炮,把炮当成点,把怪当成边,一边连 ...

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

    嗯... 题目链接:http://poj.org/problem?id=3041 这道题的思想比较奇特: 把x坐标.y坐标分别看成是二分图两边的点,如果(x,y)上有行星,则将(x,y)之间连一条边, ...

  8. HihoCoder 1122二分图二 ---最大匹配之匈牙利算法

    二分图二•二分图最大匹配之匈牙利算法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上一回我们已经将所有有问题的相亲情况表剔除了,那么接下来要做的就是安排相亲了.因为过 ...

  9. POJ 3041.Asteroids-Hungary(匈牙利算法)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23963   Accepted: 12989 Descr ...

随机推荐

  1. EF CODEFIRST WITH ORACLE 存储过程

    EF  CODEFIRST WITH ORACLE 解决存储过程一直没找到解决方案 所以最后也没办法还是用了最基本的解决方案 采用Oracle.ManagedDataAccess提供的ADO基础访问类 ...

  2. 查看hdfs各目录分别占用多少空间

    之前在网上搜索到的全部单位好像都是byte的,看起来很麻烦,然后自己看了下 hadoop fs -help [hadoop@slave3 java]$ hadoop fs -help Usage: h ...

  3. 什么是个CDN???CDN是干什么的??

    1.什么是CDN??? CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络& ...

  4. 第二天:tomcat体系结构和第一个Servlet

    1.  打war包 2.  Tomcat体系再说明:   问题:如何去配置默认主机???    3.tomcat和servlet在网络中的位置 4.    servlet快速入门案例   1).开发s ...

  5. 使用ServerSocket建立聊天服务器(一)

    -------------siwuxie095                             工程名:TestMyServerSocket 包名:com.siwuxie095.socket ...

  6. C++——virtual

    一.放在父类的函数名前面:多态 1.作用:实现多态:子类可以自定义父类中的virtual函数 #include <iostream> using namespace std; class ...

  7. MySQL update select组合

    update t_news inner join (select readCount from t_news t2 where t2.id=1) t1 set t_news.readCount = t ...

  8. 25-Fibonacci(矩阵快速幂)

    http://poj.org/problem?id=3070     Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  9. Ubuntu相关IP配置(转)

    配置文件:/etc/network/interfaces 打开后里面可设置DHCP或手动设置静态ip.前面auto eth0,让网卡开机自动挂载. 1. 以DHCP方式配置网卡 编辑文件/etc/ne ...

  10. C++笔记--异常

    引言 异常,让一个函数可以在发现自己无法处理的错误时抛出一个异常,希望它的调用者可以直接或者间接处理这个问题.而传统错误处理技术,检查到一个局部无法处理的问题时: 1.终止程序(例如atol,atoi ...