匈牙利算法

需要知道的知识点:
最小覆盖点==最大匹配
关于匈牙利算法的详解,可以看我的另外一篇博文。

例题:

Asteroids

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27787 Accepted: 14897
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).

Source

POJ3041

分析

有一个N×N的网格,网格上有小怪兽,一次操作可以清除一行或者一列的小怪兽,问至少多少次能消灭所有的。这个题目就是最小覆盖点的题,利用之前的知识点,把这个题目转化成求最大匹配的问题就是一个板子题了。

参考代码

#include <cstdio>
#include <iostream>
#include <cstring>
const int MAXN=505;
const int MAXM=10005;
using namespace std;struct Edge{int to,next;
}edge[MAXM];
int head[MAXN],tot;
int N,K,r,c;void init()
{tot=0;memset(head,-1,sizeof(head));
}void addedge(int u,int v)
{edge[tot].to=v;edge[tot].next=head[u];head[u]=tot++;
}int linker[MAXN];
bool used[MAXN];
int uN;bool dfs(int u)
{for (int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if (!used[v]){used[v]=true;if(linker[v]==-1 || dfs(linker[v])){linker[v]=u;return true;}}}return false;
}int hungary()
{int res=0;memset(linker,-1,sizeof(linker));for (int u=1;u<=uN;u++){memset(used,false,sizeof(used));if(dfs(u)) res++;}return res;
}int main()
{cin >> N >> K;init();for (int i=0;i<K;i++){scanf("%d%d",&r,&c);addedge(r,c);}uN=N;cout << hungary() << endl;return 0;
}

POJ3014(最小覆盖点;匈牙利算法)相关推荐

  1. 二分图的最大匹配—匈牙利算法

    [基本概念]: 二分图: 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分 ...

  2. 二分图的最大匹配(匈牙利算法)HDU1083

    二分图: 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别属于这两个不同 ...

  3. 二分图的最大匹配 匈牙利算法

    基本概念 1.二分图: 二分图又称作二部图,是图论中的一种特殊模型. 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相交的子集(A,B),并且图中的每条边(i,j)所关联的两个顶点i和j分别 ...

  4. [匈牙利算法] 最小点覆盖 König定理

    König定理: 一个二分图中的最大匹配数等于这个图中的最小点覆盖数. (你都能用最少的覆盖最多的还不是最大匹配数??) poj 1325 Machine Schedule [题目描述]有两部机器A和 ...

  5. 二分图匹配匈牙利算法DFS实现

    1 /*==================================================*\ 2 | 二分图匹配(匈牙利算法DFS 实现) 3 | INIT: g[][]邻接矩阵; ...

  6. 解题报告:luogu P2423 [HEOI2012]朋友圈【最大团转最大点独立集(匈牙利算法+时间戳优化)】

    图的最大团:"任意两点之间都有一条边相连"的子图被称为无向图的团,点数最多的团为图的最大团 朋友圈中任意两个点之间都有关系,既是图中的团. 答案就是图中的最大团. 我们如果把B国的 ...

  7. 二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement

    题目传送门 1 /* 2 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 3 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 4 */ 5 ...

  8. 二分图匹配的匈牙利算法

    匈牙利算法,很绕,其实写起来也就一点点长度.. bool find(int a){int i,j;for(i=head[a];i;i=next[i]){j=to[i];//获得相邻的点if(!unab ...

  9. 二分图-匈牙利算法模板

    二分图就不赘述了,我在知识资料整理有相关资料. .最大匹配  .最小路径覆盖  .最小点覆盖  .最大独立集 最大匹配:二分图中边集最大的那个匹配 最小路径(边)覆盖:用尽量小的不想交简单路径覆盖有向 ...

最新文章

  1. Python使用matplotlib绘制数据去重前后的柱状图对比图(在同一个图中显示去重操作之后同一数据集的变化情况)
  2. h2 mysql 兼容性_H2内存数据库对sql语句的支持问题 sql放到mysql数据库中能跑
  3. python global用法_14_手把手教你学Python之函数(下)
  4. [译]Speeding up your PHP scripts
  5. hdfs和日志业务系统
  6. JsonHelper(Json帮助类)
  7. Notepad++美化,关于编程主题与字体
  8. 【开源微信】微信登入公众号、小程序
  9. pdf转换器免费版下载
  10. 【论文简述及翻译】A Large Dataset to Train Convolutional Networks for Disparity, Optical Flow, and SceneFlow
  11. OI游记——一个不配称为OIer的失败选手的自白
  12. 大厂面试中HR可能会问到的问题
  13. Go: 模拟一张银行卡存、取、查的功能(综合练习)
  14. 用javascript分类刷leetcode4.贪心(图文视频讲解)
  15. SVG文档:使用SVG 编程(转自IBM文档库)
  16. 速卖帮AI点餐流程 AI菜品识别结账
  17. flask----继承和bock
  18. 数字图像处理之matlab实验(一):基本操作
  19. 百度云其实真的很不错
  20. Kaggle鱼品种识别

热门文章

  1. 本周必看 | 7月MLPython 最佳开源项目Top 10 :从几百个项目中脱颖而出,都在收藏!...
  2. 【java毕业设计】基于java+Lucene+Tomcat的搜索引擎设计与实现(毕业论文+程序源码)——搜索引擎
  3. 翻译软件哪个准确度高
  4. 前台EasyUI哪些事一
  5. Stm32 HAL_UART_Receive读取不到数据的问题
  6. 极速PEu盘启动盘制作工具(2003内核)V06.6官方版
  7. HTML基础知识汇总
  8. LED32*32点阵书写屏设计方案
  9. iOS UIColor RGB 颜色对照表
  10. CNN(卷积神经网络)在视频动作分类中的应用