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 ...
随机推荐
- jQuery.data() 与 jQuery(elem).data()源码解读
之前一直以为 jQuery(elem).data()是在内部调用了 jQuery.data(),看了代码后发现不是.但是这两个还是需要放在一起看,因为它们内部都使用了jQuery的数据缓存机制.好吧, ...
- linux网卡软中断shell脚本
LANG=C;export LANG; service irqbalance stop >/dev/null 2>&1;chkconfig irqbalance off; bon ...
- Ubuntu下安装Yarm-PM2
首先打开yarm的官网.https://www.yarnpkg.com/zh-Hant/ (一)yarn的官方安装方法: 1.上通过 Debian 套件安裝 Yarn,粘贴以下命令 curl -sS ...
- 获取spring里的bean
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring. ...
- jstl表达式的应用的条件
在el表达式中,有时我们要写for循环,这时我们要写 <c:forEach items="${list}" var="news" > </c: ...
- AngularJS(一):概述
本文也同步发表在我的公众号“我的天空” 在我们之前学习的前端代码编写过程中,总是通过HTML与CSS来进行页面布局,而使用JS来控制页面逻辑,因此,我们习惯于在JS中来操作页面元素,如以下代码,我们希 ...
- web标准、可用性、可访问性
前言:大家不难发现,只要是招聘UED相关的岗位,如前端开发工程师.交互设计师.用户研究员甚至视觉设计师,一般都对web标准.可用性和可访问性的理解有要求.那么到底什么是web标准.可用性.可访问性呢? ...
- 从程序猿到SAP产品经理,我是如何转型的?
文章作者:Jason Xia(夏建军) Jerry: 今天的文章来自Jason Xia, 我的老同事,和我一样从2007年进入SAP成都研究院工作至今.这篇文章讲述了Jason是如何从一名SAP资深开 ...
- [视觉] 基于YoloV3的实时摄像头记牌器
基于YoloV3的实时摄像头记牌器 github:https://github.com/aoru45/cards_recognition_recorder_pytorch 最终效果 数据准备 数据获取 ...
- React 官方脚手架 create-react-app快速生成新项目
进入新公司已经半年了,各个业务线,技术栈都已经熟悉,工作也已经游刃有余,决定慢下脚步,沉淀积累,回顾一下所用技术栈所包含的基本知识,以及再公司中的实战. 首先回顾新项目搭建 react脚手架目前使用较 ...