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 ;
}
 

最新文章

  1. CSS3:text-overflow实现文字截取,超出部分显示省略号
  2. UDK:AdventureKit 攀爬系统
  3. atitit. orm mapping cfg 映射配置(3)-------hbnt one2maney cfg
  4. iOS触摸事件
  5. ClassLoader,Thread.currentThread().setContextClassLoader,tomcat的ClassLoader
  6. ubuntu14.04LS中安装SSH
  7. spingmvc 返回json数据日期格式化方法
  8. Entity Framework Code First级联删除
  9. 类型与通用语言运行时:System.Object
  10. 逆向思维 UVA 11853
  11. Block 使用场景
  12. JavaWeb项目中获取对Oracle操作时抛出的异常错误码
  13. Java开发笔记(六十四)静态方法引用和实例方法引用
  14. 【mongoDB查询进阶】聚合管道(三)--表达式操作符
  15. ubuntu的应用中心打不开、闪退
  16. spring中注册bean(通过代码动态注册)
  17. iOS多线程编程之GCD介绍(转载)
  18. 搭建安卓开发环境 hello world andriod
  19. MVC使用Entity Framework Code First,用漂亮表格显示1对多关系
  20. 三十分钟理解博弈论“纳什均衡” -- Nash Equilibrium

热门文章

  1. nginx fpm生产环境的权限设置
  2. Educational Codeforces Round 51 (Rated for Div. 2)
  3. Android 环信 调用相机崩掉 mikdir()
  4. CheckPoint_vSEC_Cluster_R77.30
  5. 基于mllib的协同过滤实战(电影推荐)
  6. React开发博客系统的总结
  7. ORACLE的raw属性
  8. 2018.6.4 Oracle数据库预定义的异常列表
  9. 2018.5.14 XML文档类型定义----DTD
  10. C#做项目时的一些经验分享