Asteroids

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 16242 Accepted: 8833

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的方阵,然后有一些东西在一些坐标上,你需要用最少的炸弹把这些东西都炸掉,然后问你,最少炸弹数量是多少

题解

我们建立一个二分图,然后把每行当成一个点,把每列当成一个点,如果这一行这一列有炸弹的话,我们就建一条边。那么这道题,就转化成了,最小点覆盖问题,直接跑一大最大匹配就好了

总而言之,言而总之,我也是套版做的= =

代码

#define REP(i, n) for (int i=0;i<n;++i)
#define RD(n) scanf("%d",&n)
int V;
vector<int> G[maxn];
int match[maxn];
int used[maxn]; void add_edge(int u,int v)
{
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int v)
{
used[v]=1;
for(int i=0;i<G[v].size();i++)
{
int u=G[v][i],w=match[u];
if(w<0||!used[w]&&dfs(w))
{
match[v]=u;
match[u]=v;
return 1;
}
}
return 0;
} int bipartite_matching()
{
int res=0;
memset(match,-1,sizeof(match));
for(int v=0;v<V;v++)
{
if(match[v]<0)
{
memset(used,0,sizeof(used));
if(dfs(v))
res++;
}
}
return res;
} int n,k;
int R[maxn],C[maxn];
void solve()
{
V=n*2;
for(int i=0;i<k;i++)
{
add_edge(R[i]-1,n+C[i]-1);
}
printf("%d\n",bipartite_matching());
} int main()
{
scanf("%d%d",&n,&k);
REP(i,k)
{
RD(R[i]);
RD(C[i]);
}
solve();
return 0;
}

poj 3041 Asteroids 最小点覆盖/最大匹配的更多相关文章

  1. poj 3041 Asteroids(最小点覆盖)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  2. POJ 3041 Asteroids (最小点覆盖集)

    题意 给出一个N*N的矩阵,有些格子上有障碍,要求每次消除一行或者一列的障碍,最少消除多少次可以全部清除障碍. 思路 把关键点取出来:一个障碍至少需要被它的行或者列中的一个消除. 也许是最近在做二分图 ...

  3. [poj] 3041 Asteroids || 最小点覆盖=最大二分图匹配

    原题 本题为最小点覆盖,而最小点覆盖=最大二分图匹配 //最小点覆盖:用最少的点(左右两边集合的点)让每条边都至少和其中一个点关联. #include<cstdio> #include&l ...

  4. POJ 3041 Asteroids 最小点覆盖 == 二分图的最大匹配

    Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...

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

       Asteroids POJ - 3041 Bessie wants to navigate her spaceship through a dangerous asteroid field in ...

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

    Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...

  7. poj 3041 Asteroids (二分图的最大匹配 第一题)

    题目:http://poj.org/problem?id=3041 题意:在某个n*n的空间内,分布有一些小行星,某人在里面打炮,放一枪后某一行或某一列的行星就都没了,让求最少的打炮数. 然后把每行x ...

  8. POJ 3041 Asteroids 最小覆盖数

    http://poj.org/problem?id=3041 题目大意: 一辆宇宙飞船在一个小行星带中,你知道,这很危险.他有一种武器,可以清除掉一行或一列的小行星.问把小行星全部清除最少的武器使用次 ...

  9. POJ 3041(最小点覆盖)

    题意: 假如你如今正处在一个N*N的矩阵中,这个矩阵里面有K个障碍物,你拥有一把武器,一发弹药一次能消灭一行或一列的障碍物,求最小的弹药消灭所有障碍物 输入为:     N K 接下来有K行,每行包括 ...

随机推荐

  1. CEC2017 benchmark function调用接口

    CEC2017 benchmark function可以从这里下载. 导师最近给了个课题让我自己研究,跟智能优化算法相关的,必不可免的要用到最新的CEC2017 benchmark function, ...

  2. linux文件管理 -> 系统压缩打包

    如果希望windows和Linux互相能使用的压缩工具, 建议.zip格式 压缩的好处主要有: 节省磁盘空间占用率 节省网络传输带宽消耗 网络传输更加快捷 Linux系统常见的后缀名所对应的压缩工具 ...

  3. 数据结构之线性表(python版)

    数据结构之线性表(python版) 单链表 1.1  定义表节点 # 定义表节点 class LNode(): def __init__(self,elem,next = None): self.el ...

  4. find命令的使用

    在以.conf结尾的文件里面查找含有aaa字符串的那一行   ( -name后面可以写  "*.*"   即匹配所有的文件 ) find / -name "*.conf& ...

  5. python进阶学习之匿名函数lambda

    定义: lambda 参数列表:return(表达式) 匿名函数的表达式,只允许有一个. 匿名函数一般用于较简单的情况. 实例1: 实例2,配合高阶函数filter()使用: filter()函数是  ...

  6. Java事务管理之Hibernate

    环境与版本 Hibernate 版本:Hibernate 4.2.2 (下载后的文件名为hibernate-release-4.2.2.Final.zip,解压目录hibernate-release- ...

  7. 生成Insert语句的存储过程

    ) drop procedure [dbo].[spGenInsertSQL] GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO )) as b ...

  8. 20165203 实验二 Java面向对象程序设计

    20165203 实验二 Java面向对象程序设计 一.面向对象程序设计1--单元测试和TDD 1.实验要求 参考 (http://www.cnblogs.com/rocedu/p/6371315.h ...

  9. getch与getchar区别

    getch(): 所在头文件:conio.h 函数用途:从控制台读取一个字符,但不显示在屏幕上 getchar(): 所在头文件:stdio.h getch与getchar基本功能相同,差别是getc ...

  10. 分析Windows的死亡蓝屏(BSOD)机制

    这篇文章本来是投Freebuf的,结果没过.就贴到博客里吧,图懒得发上来了 对于Windows系统来说,被人们视为洪水猛兽的蓝屏也是一种有利于系统稳定的机制.蓝屏其实是Windows系 统的一种自查机 ...