POJ:3041-Asteroids(匈牙利算法模板)
传送门:http://poj.org/problem?id=3041
Asteroids
Time Limit: 1000MS Memory Limit: 65536K
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).
解题心得:
题意就是要将图中的 X消去,每次可以消去一行或者消去一列。可以将行和列看做一个节点,而一个点的坐标可以看成在x和y节点上连接的一条边,要求用最小的边数目将已经覆盖的节点全部覆盖。说了那么多就是一个裸地匈牙利算法。
很好懂的匈牙利算法传送门:这里写链接内容
- 匈牙利算法的核心就在一个可不可以让出一个位置,dfs可以看成向上递归,只要已经安排的点有一个可以让出一个位置就可以安放当前。
#include<stdio.h>
#include<string.h>
using namespace std;
const int maxn = 510;
int n,m;
int match[maxn];
bool maps[maxn][maxn],vis[maxn];
void init()
{
memset(maps,0,sizeof(maps));
memset(match,0,sizeof(match));
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
maps[x][y] = true;
}
}
bool dfs(int x)
{
for(int i=1;i<=n;i++)
{
if(maps[x][i] && !vis[i])
{
vis[i] = true;
if(match[i] == 0 || dfs(match[i]))//可以让出就ans++,不然就不能连接
{
match[i] = x;
return true;
}
}
}
return false;
}
int get_ans()
{
int ans = 0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(dfs(i))
ans++;
}
return ans;
}
int main()
{
while(scanf("%d%d",&n,&m) != EOF)
{
init();
int ans = get_ans();
printf("%d\n",ans);
}
return 0;
}
POJ:3041-Asteroids(匈牙利算法模板)的更多相关文章
- POJ 3041 Asteroids | 匈牙利算法模板
emmmmm 让你敲个匈牙利 #include<cstdio> #include<algorithm> #include<cstring> #define N 51 ...
- POJ 3041 Asteroids 匈牙利算法,最大流解法,行列为点 难度:1
http://poj.org/problem?id=3041 #include <cstdio> #include <cstring> #include <vector& ...
- POJ 3041.Asteroids-Hungary(匈牙利算法)
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23963 Accepted: 12989 Descr ...
- poj 1469 COURSES(匈牙利算法模板)
http://poj.org/problem?id=1469 COURSES Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...
- poj 1274 The Perfect Stall【匈牙利算法模板题】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20874 Accepted: 942 ...
- POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)
POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...
- 匈牙利算法模板 hdu 1150 Machine Schedule(二分匹配)
二分图:https://blog.csdn.net/c20180630/article/details/70175814 https://blog.csdn.net/flynn_curry/artic ...
- POJ 3041 Asteroids (对偶性,二分图匹配)
题目:POJ 3041 Asteroids http://poj.org/problem?id=3041 分析: 把位置下标看出一条边,这显然是一个二分图最小顶点覆盖的问题,Hungary就好. 挑战 ...
- poj 3041——Asteroids
poj 3041——Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22604 Accep ...
随机推荐
- tomcat7 fail to start inside Ubuntu Docker container
The tomcat startup script needs some special privileges. Concrete it needs to check all running proc ...
- hbase按照时间戳删除记录
1.按照时间戳范围查询记录 echo "scan 'event_log', { COLUMN => 'cf:sid', TIMERANGE => [1466265600272, ...
- Spark Mllib里如何程序输出数据集的条数(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第17章 决策树多元分类UCI Covertype数据集
- 关于小米手机USB传输稍大点的文件老中断的问题解决方法!
关于小米手机USB传输稍大点的文件老中断的问题解决方法! 这是一个很痛苦的事情,当你传输大文件的时候,传输到一半就会莫名其妙的中断,拔插数据线很多次以后,好不容易没准可以成功传输一次. 后来使用了36 ...
- SpringCloud服务的平滑上下线
http://blog.itpub.net/31545684/viewspace-2215300/ spring cloud eureka 参数配置 https://www.jianshu.com/p ...
- EF批量插入数据耗时对比
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Kendo MVVM 数据绑定(五) Events
Kendo MVVM 数据绑定(五) Events 本篇和 Kendo MVVM 数据绑定(三) Click 类似,为事件绑定的一般形式.Events 绑定支持将 ViewModel 的方法绑定到 D ...
- 用jQuery实现jsonp跨域
跨域的安全限制都是指浏览器端来说的.服务器端是不存在跨域安全限制的,所以通过本机服务器端通过类似httpclient方式完成“跨域访问”的工作,然后在浏览器端用AJAX获取本机服务器端“跨域访问”对应 ...
- iOS - KVO 简单应用
KVO(键值监听)全称 Key Value Observing.使用KVO可以实现视图组件和数据模型的分离,视图作为监听器,当模型的属性值发生变化后,监听器可以做相应的处理.KVO的方法由NSKeyV ...
- Android 开发干货,键盘状态
地址:http://www.imooc.com/article/4711 [A]stateUnspecified:软键盘的状态并没有指定,系统将选择一个合适的状态或依赖于主题的设置 [B]stateU ...