POJ3041Asteroids(最小点覆盖+有点小抽象)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18289 | Accepted: 9968 |
Description
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
* 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
Sample Input
3 4
1 1
1 3
2 2
3 2
Sample Output
2
Hint
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).
解题思路:
http://www.cnblogs.com/lyy289065406/archive/2011/07/30/2121730.html
把方阵看做一个特殊的二分图(以行列分别作为两个顶点集V1、V2,其中| V1|=| V2|)
然后把每行x或者每列y看成一个点,而障碍物(x,y)可以看做连接x和y的边。按照这种思路构图后。问题就转化成为选择最少的一些点(x或y),使得从这些点与所有的边相邻,其实这就是最小点覆盖问题。
关键是构图,有点抽象,需要把点当成是边。
再利用二分图最大匹配的König定理:
最小点覆盖数 = 最大匹配数
(PS:最小点覆盖:假如选了一个点就相当于覆盖了以它为端点的所有边,你需要选择最少的点来覆盖图的所有的边。)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int MAX = + ;
int g[MAX][MAX],link[MAX],vis[MAX];
int n,V1,V2,k;
int dfs(int x)
{
for(int i = ; i <= V2; i++)
{
if(vis[i] == && g[x][i])
{
vis[i] = ;
if(link[i] == || dfs(link[i]))
{
link[i] = x;
return true;
}
}
}
return false;
}
int main()
{
scanf("%d%d", &n,&k);
V2 = V1 = n;
for(int i = ; i <= k; i++)
{
int x,y;
scanf("%d%d",&x,&y);
g[x][y] = true; }
int m = ;
memset(link,,sizeof(link));
for(int i = ; i <= V1; i++)
{
memset(vis,,sizeof(vis));
if(dfs(i))
m++;
}
printf("%d\n",m);
return ;
}
POJ3041Asteroids(最小点覆盖+有点小抽象)的更多相关文章
- 【最小点覆盖】POJ3041-Asteroids
[题目大意] 在n*n的网格上有n个点,每次删除一行或者一列,问至少要删除几次才能删除完全部的这些店? [思路] 在国庆最后一天到来前,把二分图的三个基本情况[最小点覆盖][DAG图的最小路径覆盖]和 ...
- 【POJ 3041】Asteroids (最小点覆盖)
每次选择清除一行或者一列上的小行星.最少选择几次. 将行和列抽象成点,第i行为节点i+n,第j列为节点j,每个行星则是一条边,连接了所在的行列. 于是问题转化成最小点覆盖.二分图的最小点覆盖==最大匹 ...
- 【noip模拟】最小点覆盖
Time Limit: 1000ms Memory Limit: 128MB Description 最小点覆盖是指在二分图中,用最小的点集覆盖所有的边.当然,一个二分图的最小点覆盖可能有很 ...
- Ex 6_21 最小点覆盖问题_第八次作业
子问题定义: 对于图中的每个结点,有两种状态,即属于最小点覆盖和不属于最小点覆盖,定义minSet[i][0]表示结点i属于点覆盖,并且以i为根的树的最小点覆盖的大小.minSet[i][1]表示点i ...
- POJ 3041 Asteroids(最小点覆盖)题解
题意:n*n的网格中有k个点,开一枪能摧毁一行或一列的所有点,问最少开几枪 思路:我们把网格看成两个集合,行集合和列集合,如果有点x,y那么就连接x->y,所以我们只要做最小点覆盖就好了. 参考 ...
- 二分图 最小点覆盖 poj 3041
题目链接:Asteroids - POJ 3041 - Virtual Judge https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...
- [BZOJ3140][HNOI2013]消毒(二分图最小点覆盖)
3140: [Hnoi2013]消毒 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1621 Solved: 676[Submit][Status] ...
- nyoj237 游戏高手的烦恼(最小点覆盖)
题目237 题目信息 pid=237" style="text-decoration:none; color:rgb(55,119,188)">执行结果 本题排行 ...
- Cogs 1632. 搬运工(二分图最小点覆盖)
搬运工 ★ 输入文件:worker.in 输出文件:worker.out 简单对比 时间限制:1 s 内存限制:256 MB [题目描述] 小涵向小宇推荐了一款小游戏. 游戏是这样的,在一个n*n的地 ...
随机推荐
- NET Office 组件Spire
高效而稳定的企业级.NET Office 组件Spire 在项目开发中,尤其是企业的业务系统中,对文档的操作是非常多的,有时几乎给人一种错觉的是"这个系统似乎就是专门操作文档的" ...
- Spring中Bean的命名问题(id和name区别)及ref和idref之间的区别
Spring中Bean的命名 1.每个Bean可以有一个id属性,并可以根据该id在IoC容器中查找该Bean,该id属性值必须在IoC容器中唯一: 2.可以不指定id属性,只指定全限定类名,如: & ...
- [转]SQLServer跨服务器访问数据库(openrowset/opendatasource/openquery)
正 文: 1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因 ...
- c# nullable类型有什么用
可空类型,语法: ; int? inully = 10; Nullable<int> inullx0 = null; int? inully0 ...
- Android Studio Gradle编译项目报错
Gradle project sync failed Android Studio每次更新版本都会更新Gradle这个插件,但由于长城的问题每次更新都是失败,又是停止在Refreshing Gradl ...
- 一个因为粗心的Bug
/** * 数据绑定,分页显示 */ private void updataMenu(final EditText search) { if(listwz==null) { return; } pag ...
- Opencv step by step - ROI
什么是ROI?ROI就是region of interest ,就是你感兴趣的图像部分,在图像处理中,可能同时要处理多个ROI. Opencv有ROI的API,但是只能同时处理一个(书本上说的,未验证 ...
- 简便的自动布局,对UIStackView的个人理解!
序言: 更新了很久的Linux,我怕朋友们都视觉疲劳了,今天就更新在学ios开发时候,对一些知识点的理解.希望各位会喜欢! 正文: UIStackView 类提供了一个高效的接口用于平铺一行或一列的视 ...
- 快速替换dll命名空间
时15年9月18日,闲来无事,更一博. 背景 三天前,Y公司为避免法律诉讼,需要将代码(包括dll)中有关老东家的命名空间全部改掉.现在我就将快速替换命名空间的方法一步步告诉大家,注意,此举不是为了 ...
- Object C学习笔记23-继承,重写,重载
前面的学习都一直在使用Object C对象,但是没有具体总结过Object C中的对象使用特性,这里简单总结一下. 一. 继承 在面向对象编程中,子类可以通过继承得到父类的可以继承的的属性和方法,在 ...