题目链接:

http://poj.org/problem?id=3041

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

题意描述:
输入矩阵的大小和小行星的个数及坐标
计算并输出至少需要多少颗能量弹消灭掉所有的小行星
解题思路:
首先建立二分图,行和列为两个集合,有小行星的位置存入邻接矩阵为1,使用匈牙利算法,计算二分最大匹配,最后求的最小顶点覆盖。
AC代码:
 #include<stdio.h>
#include<string.h>
int n,k,e[][],pred[],queue[],cx[],cy[];
int maxmatch();
int main()
{
int i,x,y;
while(scanf("%d%d",&n,&k) != EOF)
{
memset(e,,sizeof(e));
for(i=;i<=k;i++)
{
scanf("%d%d",&x,&y);
e[x][y]=;
}
printf("%d\n",maxmatch());//输出最小顶点覆盖数
}
return ;
}
int maxmatch()
{
int i,j,y;
int cur,tail,res=;
memset(cx,0xff,sizeof(cx));
memset(cy,0xff,sizeof(cy)); for(i=;i<=n;i++)
{
if(cx[i] != -)//找到x集合中每个未盖点i进行一次找交错轨
continue; for(j=;j<=n;j++)
pred[j]=-;//初始化为-2 cur=;//队列初始化
tail=; for(j=;j<=n;j++)//将i的邻接顶点加入队列
{
if(e[i][j])
{
pred[j]=-;//-1表示遍历到,是邻接顶点
queue[tail++]=j;
}
} while(cur < tail)//BFS
{
y=queue[cur];
if(cy[y]==-)
break;//找到了一个未匹配的点,则找到了一条交错轨
cur++;
//已经匹配给cy[y]了,从cy[y]出发,将其邻接点加入队列
for(j=;j<=n;j++)
{
if(pred[j] == - && e[ cy[y ]][j])
{
pred[j]=y;
queue[tail++]=j;
}
}
}
if(cur == tail)//没有找到交错轨
continue; while(pred[y] > -)//更改交错轨上的匹配状态
{
cx[ cy[pred[y]] ] = y;
cy[y]=cy[ pred[y] ];
y=pred[y];
}
cy[y]=i;
cx[i]=y; res++;//匹配数加1
}
return res;
}

最新文章

  1. 罗辑思维(罗胖)阿瑟&#183;黑利书:《大饭店》、《晚间新闻》、《超载》、《最后诊断》、《钱商》、《身高居位》电子书 pdf和mobi格式得到下载
  2. linux下搭建sock5代理
  3. MySQL 调优基础(五) Linux网络
  4. input file美化
  5. Palindrome Partitioning II Leetcode java
  6. javaSE第九天
  7. Android中利用画图类和线程画出闪烁的心形
  8. Windows环境下Android NDK环境搭建
  9. 运行期以索引获取tuple元素-C++11之2
  10. 关于redis数据库的简单思考
  11. Java中方法定义和调用的学习
  12. centos7 端口3306无法连接问题
  13. Smack类库详细介绍
  14. C++解析七-重载运算符和重载函数
  15. 反向代理负载均衡调度:nginx
  16. 广州商学院Python正方教务系统爬虫(获取个人信息成绩课表修改密码)
  17. [Algorithm] Powerset Problem
  18. BZOJ 1009 [HNOI2008]GT考试 (KMP + 矩阵快速幂)
  19. Centos设置SSH限制登录用户及IP
  20. Git学习系列之Git基本操作克隆项目(图文详解)

热门文章

  1. 由linux命令谈学习操作系统的重要性
  2. 转 - .net/c# 使用RabbitMQ
  3. SQL Server 2016 行级别权限控制
  4. Python列表操作
  5. robotframework的学习笔记(十六)----robotframework标准库String
  6. numpy库常用基本操作
  7. 微信小程序开发之详解生命周期方法
  8. 巧用CSS实现宝马LOGO
  9. Ubuntu 搭建简单的git server
  10. sqoop数据迁移(基于Hadoop和关系数据库服务器之间传送数据)