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的地 ...
随机推荐
- html中的src与href的区别
写代码的时候就经常把这两个属性弄混淆,到底是href还是src,href标识超文本引用,用在link和a等元素上,href是引用和页面关联,是在当前元素和引用资源之间建立联系,src表示引用资源,表示 ...
- 在Function对象上扩展method方法
;(function() { /** * 在Function对象上扩展method方法 * @param {String} name 扩展的方法名称 * @param {Function} callb ...
- gridpanel分组汇总
[ExtJS5学习笔记]第三十节 sencha extjs 5表格gridpanel分组汇总 2015-05-31 86 本文地址:http://blog.csdn.net/sushengmi ...
- usb驱动开发2之代码地图
USB只是Linux庞大家族里的一个小部落,host controller是它们的族长,族里的每个USB设备都需要被系统识别.下图显示包含一个USB接口的USB鼠标导出的结果. USB系统中的第一个U ...
- TeeChart控件的安装与常用 功能设置
TeeChart控件的安装 TeeChart 7.0 With Source在Delphi 7.0中的安装 一.删除Delphi7自带TeeChart 1.Component -> insta ...
- Linux Linux程序练习十(网络编程大文件发送)
//网络编程客户端--大文件传输 #include <stdio.h> #include <stdlib.h> #include <string.h> #inclu ...
- C#属性: 利用set实现递归
直接帖代码: public class Bird { int xdata; /// <summary> /// 属性的简洁写法,等同于下面的xData方式 /// </summary ...
- php基础07:流程控制
<?php //1.PHP foreach循环只适用于数组,并用于遍历数组中的每个键/值对. $colors = array("red","green", ...
- 团购、定时抢购倒计时js版
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org ...
- Jenkins入门系列之——00答疑解惑
写在最前的总结:Jenkins其实就是一个工具,这个工具的作用就是调用各种其他的工具来达成你的目的.比如你要获取Subversion上最新的源代码,Jenkins会去调用SVNKIT(插件的核心Jar ...