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 ...
随机推荐
- JAVA基础之Map接口
个人理解: 个人感觉Map接口以后会经常用到,可以很清晰地查找某个对象,要熟悉其四种遍历方法.特别注意其键值对的键不能是重复的,有想定义有序存取的话,可以使用LinkedHashMap集合.不过定义自 ...
- layui内置loading等待加载
点击功能按钮之后 var loading = layer.load(0, { shade: false, time: 2*1000 }); 参数: icon:0,1,2 loading风格 shade ...
- Hive基础(1)
Hive基础(1) Hive的HQL(2) 1. Hive并不是分布式的,它独立于机器之外,类似于Hadoop的客户端. 2. 元数据和数据的区别,前者如表名.列名.字段名等. 3. Hive的三种安 ...
- Python+Selenium与Chrome如何进行完美结合
zhuan:http://blog.51cto.com/starpoint/2102975?cid=704621 使用WebDriver在Chrome浏览器上进行测试时,需要从http://chrom ...
- get_user
Name get_user -- Get a simple variable from user space. Synopsis get_user ( x, ptr); Arguments x ...
- LR使用流程简介之录制方式说明
1.LR脚本录制方式说明1)HTML-based script基于HTML的脚本 从内存中读取并下载资源,较少的关联处理,可以加入图片检查,回放时需要解析返回的信息 a-基于用户行为的方式 web_l ...
- HDU 2149 Public Sale 拍卖(巴什博弈)
思路:只要能给对方留下n+1,我就能胜,否则败. #include <iostream> #include <cstdio> using namespace std; int ...
- phar打包项目压力对比测试
工具 http_load 测试url: http://api.test.chaoma.me/agent/ad/good_goods/query http://api.test.chaoma.me/ag ...
- Android(java)学习笔记121:BroadcastReceiver之 自定义广播
广播使用: 电台:对外发送信号.---------电台发送广播(可以自定义) 收音机:接收电台的信号.-----广播接收者 这里,我们就说明自定 ...
- [神经网络]一步一步使用Mobile-Net完成视觉识别(四)
1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第四篇,下载预训练模型并训练自己的数据集. 前面我们配置好了labelmap,下面我们开始下载训练好的 ...