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

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

Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 14371   Accepted: 7822

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

匈牙利算法!

!!

主要是要懂得将问题转化。

将行列转化为二分两个集合。然后坐标点(i,j)即化为i行j列可以匹配。最后找出最少的覆盖点,由匈牙利定理:最少覆盖点=最大匹配边 。

假设还是不懂能够參考这里

AC代码例如以下:

#include
#include
using namespace std;int v[505],s[505],map[505][505];int n,m;int xyl(int a){ int i; for(i=1;i<=n;i++) { if(!v[i]&&map[a][i]) { v[i]=1; if(!s[i]||xyl(s[i])) { s[i]=a; return 1; } } } return 0;}int main(){ int i,j; int a,b; while(cin>>n>>m) { memset(s,0,sizeof s); for(i=1;i<=m;i++) { cin>>a>>b; map[a][b]=1; } int sum=0; for(i=1;i<=n;i++) { memset(v,0,sizeof v); if(xyl(i)) sum++; } cout<
<

转载地址:http://arrdm.baihongyu.com/

你可能感兴趣的文章
[20151201]备份迁移sql profile.txt
查看>>
【FSFA 读书笔记】Ch4 Volume Analysis & Cr 5 PC-based Partitions
查看>>
penoffice2.4安装参考
查看>>
win7下硬盘安装win7+CentOS双系统方法
查看>>
[译]JavaScript规范-葵花宝典
查看>>
引用 - PHP手册笔记
查看>>
几个问题的思考
查看>>
hidden&nbsp;change事件
查看>>
SQLServer 扫盲
查看>>
No handler for type [text] declared on field [content]
查看>>
Timer Swing
查看>>
关于软件研发的一些体会总结
查看>>
Ffmpeg移植S3C2440
查看>>
Android Studio "佛祖保佑 永无bug" 注释模板设置详解(仅供娱乐)
查看>>
黄牛都看不上 iPhone 8,我们找了 8 个人来聊聊为什么
查看>>
Java 并发/多线程教程(九)-线程安全和共享资源
查看>>
inotify
查看>>
EpPlus读取生成Excel帮助类+读取csv帮助类+Aspose.Cells生成Excel帮助类
查看>>
必看!苹果发布Face ID白皮书,一文消掉你的所有疑虑
查看>>
我的OCM之路
查看>>