poj 3041 Asteroids 题解
|
Asteroids
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 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: Source |
[Submit] [Go Back] [Status] [Discuss]
——————————————————-——————————我是分割线——————————————————————————————————————
二分图最大匹配。
一个小行星,要么清理该行,要么该列。
所以也就是每个小行星对应的行列中至少选择一样来清理。
下面建图,如果我们把每行看成集合一中的点,每列看成集合二中的点,一个小行星看成是其对应行列的连线,那么也就是说不能存在某一条连线两边的点都没有被选中的情况。
这恰好就是二分图最小点集覆盖的要求。
再利用二分图最大匹配的König定理:
最小点覆盖数 = 最大匹配数
因此本题自然转化为求 二分图的最大匹配问题
/*
Problem:
OJ:
User:
Time:
Memory:
Length:
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<cassert>
#include<climits>
#include<vector>
#include<list>
#include<map>
#define maxn 501
#define F(i,j,k) for(int i=j;i<=k;i++)
#define M(a,b) memset(a,b,sizeof(a))
#define FF(i,j,k) for(int i=j;i>=k;i--)
#define inf 0x7fffffff
#define maxm 2016
#define mod 1000000007
//#define LOCAL
using namespace std;
int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k;
int g[maxn][maxn];
int px[maxn],py[maxn];
bool vis[maxn];
inline int path(int u)
{
F(i,,n){
if(g[u][i]&&!vis[i])
{
vis[i]=true;
if(py[i]==-||path(py[i]))
{
px[u]=i;
py[i]=u;
return ;
}
}
}
return ;
}
inline int solve()
{
int cnt=;
M(px,-);M(py,-);
F(i,,n){
if(px[i]==-){
M(vis,false);
cnt+=path(i);
}
}
return cnt;
}
int main()
{
std::ios::sync_with_stdio(false);//cout<<setiosflags(ios::fixed)<<setprecision(1)<<y;
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
cin>>n>>k;
F(i,,k){
int x,y;
cin>>x>>y;
g[x][y]=;
}
cout<<solve()<<endl;
return ;
}
poj 3041 Asteroids 题解的更多相关文章
- POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)
POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...
- 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 ...
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...
- poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3041 Asteroids 二分图
原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- poj 3041 Asteroids(最小点覆盖)
http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3041 Asteroids(最小点覆盖)题解
题意:n*n的网格中有k个点,开一枪能摧毁一行或一列的所有点,问最少开几枪 思路:我们把网格看成两个集合,行集合和列集合,如果有点x,y那么就连接x->y,所以我们只要做最小点覆盖就好了. 参考 ...
- POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))
题目链接: http://poj.org/problem?id=3041 Description Bessie wants to navigate her spaceship through a da ...
随机推荐
- 【洛谷】P4207 [NOI2005]月下柠檬树
题解 原来自适应simpson积分是个很简单的东西! 我们尝试分析一下影子,圆的投影还是圆,圆锥的尖投影成一个点,而圆台的棱是圆的公切线,我们把圆心投影出来,发现平面上圆心的距离是两两高度差/tan( ...
- CSUOJ 1011 Counting Pixels
Description Did you know that if you draw a circle that fills the screen on your 1080p high definiti ...
- UVA - 11995 - I Can Guess the Data Structure! STL 模拟
There is a bag-like data structure, supporting two operations: 1 x Throw an element x into the bag. ...
- CentOS7启动流程
同样我也找了一张CentOS7的启动流程图. 第一步.硬件启动阶段 这一步和CentOS6差不多,详细请看CentOS6启动流程(含详细流程图) 第二步.GRUB2引导阶段 从这一步开始,CentOS ...
- CLR基础
一.各个语言的长处 ①非托管C/C++可对系统进行低级控制.可完全按照自己的想法管理内存,必要时方便地创建线程②使用Microsoft Visual Basic 6.0可以快速生成UI应用程序,并可以 ...
- Wireshark数据抓包教程之认识捕获分析数据包
Wireshark数据抓包教程之认识捕获分析数据包 认识Wireshark捕获数据包 当我们对Wireshark主窗口各部分作用了解了,学会捕获数据了,接下来就该去认识这些捕获的数据包了.Wiresh ...
- Python csv模块的使用
1.csv简介 CSV (Comma Separated Values),即逗号分隔值(也称字符分隔值,因为分隔符可以不是逗号),是一种常用的文本 格式,用以存储表格数据,包括数字或者字符.很多程序在 ...
- Django的orm中get和filter的不同
Django的orm框架对于业务复杂度不是很高的应用来说还是不错的,写起来很方面,用起来也简单.对于新手来说查询操作中最长用的两个方法get和filter有时候一不注意就会犯下一些小错误.那么今天就来 ...
- Go语言特点
作者:asta谢链接:https://www.zhihu.com/question/21409296/answer/18184584来源:知乎 1.Go有什么优势 可直接编译成机器码,不依赖其他库,g ...
- hdu 1698 线段树 成段更新
题意:一段钩子,每个钩子的值为1,有若干更新,每次跟新某段的值,若干查询某段的和 基础题了 #include<cstdio> #include<iostream> #inclu ...