Asteroids

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 24789   Accepted: 13439

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).

题意

一个n*n的网格,每个网格中有一些点,每次可以消灭一行或者一列的点,求最少的次数消灭所有的点。

分析

这是一个二分图,行和列分别是两个集合。求最少点覆盖集。

结论:最小点覆盖集=二分图最大匹配数

建图跑最大流即可。

建图:S向行连流量为1的边,列向T连流量为1的边,对于每个点(u,v),由u向v连流量为1的边

code

 #include<cstdio>
#include<algorithm>
#include<cstring> using namespace std;
const int N = ;
const int INF = 1e9;
struct Edge{
int to,nxt,c;
Edge() {}
Edge(int x,int y,int z) {to = x,c = y,nxt = z;}
}e[];
int q[],L,R,S,T,tot = ;
int dis[N],cur[N],head[N]; inline char nc() {
static char buf[],*p1 = buf,*p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,,stdin),p1==p2) ? EOF :*p1++;
}
inline int read() {
int x = ,f = ;char ch=nc();
for (; ch<''||ch>''; ch=nc()) if(ch=='-')f=-;
for (; ch>=''&&ch<=''; ch=nc()) x=x*+ch-'';
return x*f;
}
void add_edge(int u,int v,int c) {
e[++tot] = Edge(v,c,head[u]);head[u] = tot;
e[++tot] = Edge(u,,head[v]);head[v] = tot;
}
bool bfs() {
for (int i=; i<=T; ++i) cur[i] = head[i],dis[i] = -;
L = ,R = ;
q[++R] = S;dis[S] = ;
while (L <= R) {
int u = q[L++];
for (int i=head[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (dis[v] == - && e[i].c > ) {
dis[v] = dis[u]+;q[++R] = v;
if (v==T) return true;
}
}
}
return false;
}
int dfs(int u,int flow) {
if (u==T) return flow;
int used = ;
for (int &i=cur[u]; i; i=e[i].nxt) {
int v = e[i].to;
if (dis[v] == dis[u] + && e[i].c > ) {
int tmp = dfs(v,min(flow-used,e[i].c));
if (tmp > ) {
e[i].c -= tmp;e[i^].c += tmp;
used += tmp;
if (used == flow) break;
}
}
}
if (used != flow) dis[u] = -;
return used;
}
int dinic() {
int ret = ;
while (bfs()) ret += dfs(S,INF);
return ret;
} int main() {
int n = read(),m = read();
S = n + n + ,T = n + n + ;
for (int i=; i<=n; ++i) add_edge(S,i,),add_edge(i+n,T,);
for (int i=; i<=m; ++i) {
int u = read(),v = read();
add_edge(u,v+n,);
}
int ans = dinic();
printf("%d",ans);
return ;
}
 

POJ 3041 Asteroids (二分图最小点覆盖集)的更多相关文章

  1. POJ 3041 Asteroids (二分图最小点覆盖)

    题目链接:http://poj.org/problem?id=3041 在一个n*n的地图中,有m和障碍物,你每一次可以消除一行或者一列的障碍物,问你最少消除几次可以将障碍物全部清除. 用二分图将行( ...

  2. POJ 3041 Asteroids(最小点覆盖集)

                                                                      Asteroids Time Limit: 1000MS   Mem ...

  3. poj 3041 Asteroids(最小点覆盖)

    题意: N*N的矩阵,有K个敌人,坐标分别是(C1,C1),.....,(Rk,Ck). 有一个武器,每发射一次,可消掉某行或某列上的所有的敌人. 问消灭所有敌人最少需要多少发. 思路: 二分建图:左 ...

  4. POJ 3041 Asteroids(最小点覆盖)题解

    题意:n*n的网格中有k个点,开一枪能摧毁一行或一列的所有点,问最少开几枪 思路:我们把网格看成两个集合,行集合和列集合,如果有点x,y那么就连接x->y,所以我们只要做最小点覆盖就好了. 参考 ...

  5. POJ 2226 Muddy Fields (最小点覆盖集,对比POJ 3041)

    题意 给出的是N*M的矩阵,同样是有障碍的格子,要求每次只能消除一行或一列中连续的格子,最少消除多少次可以全部清除. 思路 相当于POJ 3041升级版,不同之处在于这次不能一列一行全部消掉,那些非障 ...

  6. POJ2226 Muddy Fields(二分图最小点覆盖集)

    题目给张R×C的地图,地图上*表示泥地..表示草地,问最少要几块宽1长任意木板才能盖住所有泥地,木板可以重合但不能盖住草地. 把所有行和列连续的泥地(可以放一块木板铺满的)看作点且行和列连续泥地分别作 ...

  7. POJ1325 Machine Schedule(二分图最小点覆盖集)

    最小点覆盖集就是在一个有向图中选出最少的点集,使其覆盖所有的边. 二分图最小点覆盖集=二分图最大匹配(二分图最大边独立集) 这题A机器的n种模式作为X部的点,B机器的m种模式作为Y部的点: 每个任务就 ...

  8. Asteroids POJ - 3041 匈牙利算法+最小点覆盖König定理

    题意: 给出一个N*N的地图N   地图里面有K个障碍     你每次可以选择一条直线 消除这条直线上的所有障碍  (直线只能和列和行平行) 问最少要消除几次 题解: 如果(x,y)上有一个障碍 则把 ...

  9. POJ 3041 Asteroids 二分图

    原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. 【收藏】这么多WEB组件(CSS),攒一个网站够了吧?

    简言 总是喜欢简单又精致的东西,美的不繁复也不张扬.这是闷骚程序员的癖好么?闲来无事,把收集到的部分WEB组件整理汇总一下,攒一个逼格高一点的网站够了吧? 1 表单(form)相关 1.1 输入框(i ...

  2. java集合杂谈

    一:java集合框架如下图所示: 大致说明:看上面的框架图,先抓住它的主干,即Collection和Map. 1.Collection是一个接口,是高度抽象出来的集合,它包含了集合的基本操作和属性.C ...

  3. 原生ajax瀑布流demo

    最近听朋友们说起瀑布流挺多的,自己就去研究下了,一个简单的原生demo,分享给大家... 简单分为三个文档,有详细的注释:img:ajax.php:demo.php 其中img文件夹中放入图片 1.j ...

  4. 工作中遇到的有关echarts地图和百度地图的问题

    工作中遇到的有关echarts地图和百度地图的问题 *** 前言:在做项目中需要制作一个场景是左边是柱状图,右边是地图,地图上悬浮一个按钮可以切换echarts地图和百度地图.*** 功能: 在点击左 ...

  5. 调用cmd命令行命令(借鉴)

    留待以后观看 ———————————————————————————————————————————————————————————————————————————— public class IP_ ...

  6. 8. String to Integer

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. bit Byte KB MB GB TB 单位换算

    1TB = 1024G 1G = 1024M 1M = 1024K 1K = 1024 byte 1 byte = 8 bit

  8. 2018.6.16 PHP小实验

    PHP实验 实验一 <?php /** * Created by PhpStorm. * User: qichunlin * Date: 2018/5/17 * Time: 下午5:35 */ ...

  9. 管道命令'|' 和xargs find命令找到后把所有找到的删除

    管道符号,是unix功能强大的一个地方,符号是一条竖线:"|", 用法: command 1 | command 2 他的功能是把第一个命令command 1执行的结果作为comm ...

  10. SSM框架之动态代理Mapper快速搭建

    1.   新建Maven项目 ssmnew 2.    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...