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 ...
随机推荐
- 教你如何清除 MyEclipse/Eclipse 中 Web Browser 和 Switch Workspace 的历史记录
有些许强迫症的开发人员可能会因为 MyEclipse/Eclipse 中 Web Browser 和 Switch Workspace 冗余的历史记录而感到苦恼,下面的方法就可以有效的帮助解决你的痛点 ...
- HTML Input 表单校验之datatype
凡要验证格式的元素均需绑定datatype属性,datatype可选值内置有10类,用来指定不同的验证格式. 如果还不能满足您的验证需求,可以传入自定义datatype,自定义datatype是一个非 ...
- 谷歌pagerank算法简介
在这篇博客中我们讨论一下谷歌pagerank算法.这是参考的原博客连接:http://blog.jobbole.com/71431/ PageRank的Page可是认为是网页,表示网页排名,也可以认为 ...
- Ext.example.msg()应用
①需要在开发包中包含文件夹example/shared中的example.js和example.css两个文件即可. ②在html文件中引入: <script src="../extj ...
- Bunch 转换为 HDF5 文件:高效存储 Cifar 等数据集
关于如何将数据集封装为 Bunch 可参考 关于 『AI 专属数据库的定制』的改进. PyTables 是 Python 与 HDF5 数据库/文件标准的结合.它专门为优化 I/O 操作的性能.最大限 ...
- select 1 from dual
begin 2018年7月14日15:06:29 select 1 from dual Oracle下的select 1 from dual 今天在看公司代码的时候,发现有这一句SQL: select ...
- CSUOJ 1781 阶乘除法
Description 输入两个正整数 n, m,输出 n!/m!,其中阶乘定义为 n!= 1*2*3*...*n (n>=1). 比如,若 n=6, m=3,则 n!/m!=6!/3!=720 ...
- POJ - 1329 Circle Through Three Points 求圆
Circle Through Three Points Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4112 Acce ...
- CF1042C Array Product 分类讨论+贪心
考虑有无负数(负数的个数为奇视作“有”,否则为“无”)和有无零 无负数无零,全部合并即可 无负数有零,那么把零合并起来,删掉零 有负数无零,把最大的负数找出来,删掉,合并剩余的数 有负数有零,把零和最 ...
- 处理QMenu的triggered信号时遇到的一个问题
最近,在一个Qt程序中使用QMenu类时,遇到了一个小问题,特记录下.首先,我模仿一下问题出现的场景:假设我在做一个高大上的XX管理系统,比如说:学生信息管理系统.在这个系统中,学生的各项信息(比如: ...