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

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的网格,网格上有星星,打一次枪可以消灭一行或者一列。
问最少打多少次枪,消灭所有的星星。
 
 思路很神奇,可以用匈牙利算法写 。
把行当成一个集合,列当成一个集合。
求最小覆盖点,也就是求最大匹配。
思路很好,当然也可以用其他的写,在学二分图,就用匈牙利啦。
 
代码(直接改的hdu的2063,嘎嘎嘎):
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <ctime>
#include <vector>
using namespace std;
const int maxn= ;
const int maxm=+;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int n,m;
int match[maxm];
bool visited[maxm];
bool mp[maxm][maxm];
bool Find(int x){
for(int i=;i<=n;i++){
if(mp[x][i]&&visited[i]==){
visited[i]=true;
if(match[i]==||Find(match[i])){
match[i]=x;
return true;
}
}
}
return false;
}
int main(){
int x,y,num;
while(~scanf("%d%d",&n,&m)){
memset(mp,,sizeof(mp));
memset(match,,sizeof(match));
memset(visited,,sizeof(visited));
for(int i=;i<m;i++){
scanf("%d%d",&x,&y);
mp[x][y]=true;
}
num=;
for(int i=;i<=n;i++){
memset(visited,,sizeof(visited));
if(Find(i))num++;
}
printf("%d\n",num);
}
return ;
}

溜啦溜啦,去写多校的二分图的题啦。

 
 

POJ 3041.Asteroids-Hungary(匈牙利算法)的更多相关文章

  1. POJ 3041 Asteroids(匈牙利+邻接表)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  2. 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids

    题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...

  3. POJ 3041 Asteroids (对偶性,二分图匹配)

    题目:POJ 3041 Asteroids http://poj.org/problem?id=3041 分析: 把位置下标看出一条边,这显然是一个二分图最小顶点覆盖的问题,Hungary就好. 挑战 ...

  4. POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)

    POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...

  5. poj 3041——Asteroids

    poj       3041——Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22604   Accep ...

  6. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...

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

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

  8. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

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

  9. POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))

    题目链接: http://poj.org/problem?id=3041 Description Bessie wants to navigate her spaceship through a da ...

  10. POJ:3041-Asteroids(匈牙利算法模板)

    传送门:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Description Bes ...

随机推荐

  1. CentOS-6.3-minimal安装gnome桌面环境(转载)

    最近,想学着搞搞linux,从入门安装开始,先装centos6.3-minimal,发现是windowser最不习惯的命令界面,先升级桌面,教程如下. 1.添加一个普通用户,并设置密码useradd  ...

  2. Linux之Permission denied没有权限

    在Linux上启动solr时,出现-bash: ./solr: Permission denied的问题. 最简单的解决方式: chmod 777 solr 傻瓜式直接赋予权限

  3. 浅谈 css 之 position用法

    在 css中, position 属性有四个值可用: static(默认值).absolute.relative.fixed. relative:相对定位(相对于自身进行在常规流中的位置进行定位,保留 ...

  4. ASP.NET Core API ---状态码

    摘录自:https://www.cnblogs.com/cgzl/p/9047626.html 状态码是非常重要的,因为只有状态码会告诉API的消费者: 请求是否如预期的成功,或者失败 如果出现了错误 ...

  5. 解决windows文件名过长无法删除的问题

    删除windows文件时,系统提示如下错误: 从网上找到下面的一种方法,顺利解决(原理不清楚),现记录删除方法如下: . 在要删除的文件夹(delete_dir)同级新建一个空文件夹(empty_di ...

  6. 以太访solidity常用的函数有哪些

    以太坊:什么是ERC20标准? 不以规矩,不能成方圆 许多人应该都听过 代码即法律(Code Is Law),因为程序写完了,无论执行多少次都会得到同样的结果,除非有外界因素的干扰.在多人协作的过程中 ...

  7. VMware下Linux配置局域网和外网访问

    我想尝试的是利用本机的ip+port来访问虚拟机上的web服务器,因为这样的话,我就能够将我的web服务器部署成为一个能让外网访问的服务器了,首先说下我的环境: 主机:系统win7,ip地址172.1 ...

  8. 爬虫:Scrapy11 - Logging

    Scrapy 提供了 log 功能.可以通过 scrapy.log 模块使用.当前底层实现使用了 Twisted logging,不过可能在之后会有所变化. log 服务必须通过显式调用 scrapy ...

  9. android 拓展ImageView播放GIF动画

    原生Android控件ImageView并不能支持播放GiF格式的图片.如果将一张GIF的图片放入ImageView中,它只会显示图片的第一帧,不会产生任何动画效果. Android中播放GIF动画实 ...

  10. 【bzoj4001】[TJOI2015]概率论 生成函数+导数

    题目描述 输入 输入一个正整数N,代表有根树的结点数 输出 输出这棵树期望的叶子节点数.要求误差小于1e-9 样例输入 1 样例输出 1.000000000 题解 生成函数+导数 先考虑节点个数为$n ...