题目很简单,但是需要推到出二分图最大匹配 = 最小覆盖

最小覆盖:证明过程http://blog.sina.com.cn/s/blog_51cea4040100h152.html

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).

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int n;
int map[502][502];
bool visited[502];
int match[502]; bool find(int i) ///查找当前的i是否可以匹配
{
int j;
for(j=1;j<=n;j++)
{
if(map[i][j]&&!visited[j])
{
visited[j]=1;
if(match[j]==-1||find(match[j]))
{
match[j]=i;
return 1;
}
}
}
return 0;
}
int main()
{
int k,i,x,y,ans;
while(~scanf("%d%d",&n,&k))
{
ans=0;
memset(map,0,sizeof(map));
memset(match,-1,sizeof(match)); for(i=0;i<k;i++)//对有意思的进行初始化
{
scanf("%d%d",&x,&y);
map[x][y]=1;
}
for(i=1;i<=n;i++)
{
memset(visited,0,sizeof(visited));//开始标记为全部没有访问
if(find(i)) ans++;
}
printf("%d\n",ans);
}
return 0;
}

Asteroids (最小覆盖)的更多相关文章

  1. Asteroids(匈牙利算法入门)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16211   Accepted: 8819 Descri ...

  2. POJ3041轰炸行星(匈牙利算法 最小覆盖点集)

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

  3. POJ2185Milking Grid(最小覆盖子串 + 二维KMP)

    题意: 一个r*c的矩形,求一个子矩形通过平移复制能覆盖整个矩形 关于一个字符串的最小覆盖子串可以看这里http://blog.csdn.net/fjsd155/article/details/686 ...

  4. UVA 11419SAM I AM(输出 最小覆盖点 )

    参考博客:如何找取 最小覆盖点集合 题意:R*C大小的网格,网格上面放了一些目标.可以再网格外发射子弹,子弹会沿着垂直或者水平方向飞行,并且打掉飞行路径上的所有目标,计算最小多少子弹,各从哪些位置发射 ...

  5. 二维KMP - 求字符矩阵的最小覆盖矩阵 - poj 2185

    Milking Grid Problem's Link:http://poj.org/problem?id=2185 Mean: 给你一个n*m的字符矩阵,让你求这个字符矩阵的最小覆盖矩阵,输出这个最 ...

  6. KMP算法 - 求最小覆盖子串

    KMP与最小覆盖子串 最小覆盖子串:对于某个字符串s,它的最小覆盖子串指的是长度最小的子串p,p满足通过自身的多次连接得到q,最后能够使s成为q的子串. 比如: 对于s="abcab&quo ...

  7. POJ 3041 Asteroids

     最小点覆盖数==最大匹配数 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12678 Accepted:  ...

  8. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. hdu---(1054)Strategic Game(最小覆盖边)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. jquery显示隐藏toggle

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Python教程:[69]strip()函数详解

    strip()用于裁剪字符串首尾的某些字符,是一个用处非常多的函数,今天我们来通过例子来探讨一下它的基本用法: 假如有一个这样的字符串 strip()不带任何参数,可以删除首位的空格 但是strip( ...

  3. js net 除法取整

    1.js中 在编程运算中,除法取整数是比较常用的!一般的编程语言都有内置的函数,JS 脚本也不例外.在JavaScript 中,实现除法取整数有两种方法,即是两个内置函数:Math.floor 和Ma ...

  4. com.opensymphony.module.sitemesh.filter.PageFilter 装饰页面

    1.web.xml中配置: <filter> <filter-name>sitemeshFilter</filter-name> <filter-class& ...

  5. oracle 行转列的例子

    with test as(select '1' bit from dual union select '0' from dual )select replace(sys_connect_by_path ...

  6. 详解@Autowired、@Qualifier和@Required

    A.@Autowired org.springframework.beans.factory.annotation.Autowired public @interface Autowired Mark ...

  7. jquery组件团购倒计时功能

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. (转载)CentOS6下 源代码方式安装openERP7.0

    CentOS6下 源代码方式安装openERP7.0 安装背景 :CPU32 bit,CentOS 6.4版本,openERP7.0,linux shell为bash,PostgreSQL9.2 1. ...

  9. HTML <div> 和 <span>

    可以通过 <div> 和 <span> 将 HTML 元素组合起来. HTML 块元素 大多数 HTML 元素被定义为块级元素或内联元素. 编者注:“块级元素”译为 block ...

  10. phonegap 2.7 ios配置安装详细教程(2.9通用)

    原地址:http://www.cnblogs.com/yansi/archive/2013/05/14/3078222.html 在移动开发日益激烈的情况下我也不得不硬着头皮尝试下新鲜的html5的a ...