博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3041 Asteroids
阅读量:5148 次
发布时间:2019-06-13

本文共 2223 字,大约阅读时间需要 7 分钟。

 

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 41 11 32 23 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).

代码:

#include 
#include
#include
#include
#include
using namespace std;const int maxn = 550;int N, K;int mp[maxn][maxn];int vis[maxn], match[maxn];int path(int u) { for(int v = 1; v <= N; v ++) { if(mp[u][v] && !vis[v]) { vis[v] = 1; if(match[v] == -1 || path(match[v])) { match[v] = u; return 1; } } } return 0;}int main() { memset(mp, 0, sizeof(mp)); memset(match, -1, sizeof(match)); scanf("%d%d", &N, &K); for(int i = 0; i < K; i ++) { int x, y; scanf("%d%d", &x, &y); mp[x][y] = 1; } int cnt = 0; for(int i = 1; i <= N; i ++) { memset(vis, 0, sizeof(vis)); if(path(i)) cnt ++; else continue; } printf("%d\n", cnt); return 0;}

  匈牙利算法 求最小点集覆盖 

转载于:https://www.cnblogs.com/zlrrrr/p/10669651.html

你可能感兴趣的文章
java行距getprinter_java 打印 类似打印存折的打印
查看>>
java php公用加密_php 和 java共用的加密方法
查看>>
java 链表指针_链表中的指针
查看>>
java培训没学好想放弃_我这么努力,为什么没学好java【java培训班分享】
查看>>
php 文档阅读,PHP实现简单在线阅读PDF文件
查看>>
php 面向对象 特性,什么是php面向对象及面向对象的三大特性
查看>>
php 错误 异常,一个显示效果非常不错的PHP错误、异常处理类
查看>>
php无法打开txt文件类型,电脑文本文档不显示txt怎么办
查看>>
vue请求php验证,PHP开发API接口签名及验证
查看>>
php查询mysql数据库生成xml,如何连接MYSQL数据库生成XML文档(2)
查看>>
php判断是不是一个数组中,php如何判断一个值是否在数组中
查看>>
apache下html无法连接到php,Ubuntu服务器下apache2无法解析html中的php代码的问题
查看>>
php内核探索,php内核探索 [转]
查看>>
blink php,Blink 中文教程
查看>>
ai分析java异常,最常见的10种Java异常问题!
查看>>
php 验证码 库,php通过GD库实现验证码功能
查看>>
Gmail规则正则表达java,常用正则表达式
查看>>
cms java内存,深入理解java虚拟机——垃圾收集器与内存分配策略
查看>>
java中bc是什么,Java中BC系统搭建制作是如何实现字符串的
查看>>
绝对素数matlab,求助:求所有三位的绝对素数的程序
查看>>