POJ3041 最小点覆盖 最大匹配数 回顾匈牙利算法
来源:互联网 发布:淘宝装修助手破解版 编辑:程序博客网 时间:2024/05/16 09:56
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.
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.
* 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
这道题就是求最小点覆盖,而最小点覆盖=最大匹配数,这道题我已经做了五遍了,每次都是对匈牙利算法的一个回顾,这是一道非常经典的题目。
匈牙利算法求最小点覆盖,也就是最大匹配数的算法流程如下:
for(int i=1;i<=xNum;i++){for(int j=1;j<=yNum;j++){ifVisited[j]=false''}if(dfs(i)==true){number++;}}bool dfs(int i){for(int j=1;j<=yNum;j++){if(ifVisited[j]==false&&grid[i][j]==true){ifVisited[j]==true;if(ancestor[j]==0||dfs(ancestor[j])==true){ancestor[j]=i;return true;}}}return false;}
本题的做法就类似于上面写的这个算法,做好输入输出后就可以写了,代码未测试,只是作为练手之用。
#include<iostream>using namespace std;int xNum,yNum,ancestor[1000],n,k;bool grid[1000][1000];bool ifVisited[1000];bool dfs(int i){for(int j=1;j<=yNum;j++){if(ifVisited[j]==false&&grid[i][j]==true){ifVisited[j]==true;if(ancestor[j]==0||dfs(ancestor[j])==true){ancestor[j]=i;return true;}}}return false;}int main(){cin>>n>>k;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++)grid[i][j]==false;} for(int i=1;i<=k;i++){int a,b;cin>>a>>b;grid[a][b]=true;}xNum=yNum=n;for(int i=1;i<=xNum;i++){for(int j=1;j<=yNum;j++){ifVisited[j]=false;}if(dfs(i)==true){number++;}}cout<<number;return 0;}
0 0
- POJ3041 最小点覆盖 最大匹配数 回顾匈牙利算法
- poj3041 最小点覆盖即最大匹配(匈牙利算法)(模板)
- POJ3041《Asteroids》方法:匈牙利算法(最小点覆盖数==最大二分匹配数)
- POJ3041 二分图(性质)最小点覆盖等于最大匹配数(匈牙利模板题)
- poj3041 最小点覆盖==二分图最大匹配 匈牙利算法求解最大匹配问题(运用DFS)
- 最大匹配(1)--poj3041(最大匹配的应用 最大匹配=最小点覆盖数)
- poj3041_匈牙利算法_二分图最小覆盖点(最大匹配数)
- 模板_matlab 匈牙利算法(最大匹配数/最小覆盖点)
- 最小点集覆盖与最大匹配之匈牙利算法
- uva11419 【最大二分匹配求最小点覆盖 匈牙利算法】
- 匈牙利算法,求最大匹配数即最小顶点覆盖
- POJ3041最大匹配匈牙利算法
- POJ3041 二分图最小点覆盖(匈牙利算法)
- poj3041 Asteroids 最小点覆盖 二分图 匈牙利算法BFS
- poj3041-Asteroids(最小点覆盖,最大匹配)
- 彻底搞定二分图的匈牙利算法,最大匹配数(最小覆盖数)、最大独立数、最小路径覆盖
- poj3041 匈牙利算法 二分图最大匹配
- poj 1325 Machine Schedule(二部图最小点覆盖集 最大匹配 匈牙利算法)(简单)
- 八种基本类型变量
- CXF开发webservice需要的最少jar包,CXF发布和调用客户端代码
- 树状数组
- Pentaho 6.0 安装与启动教程
- Java 环境搭建
- POJ3041 最小点覆盖 最大匹配数 回顾匈牙利算法
- 我的SLAM 探索(一)--如何正确地驱动Kinect V2
- 1037. Magic Coupon (25)
- 最小生成树之普里姆算法
- 第二周第三项目——小试循环
- jq延迟的使用
- Ibatis:There is no READABLE property named 'roomNo' in class 'java.lang.String'
- linux 文件描述符的一些底层实现
- 剑指offer——面试题9计算斐波纳切第n个数