HDU1045(KB10-A 二分图最大匹配)
Fire Net
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12575 Accepted Submission(s): 7614
Problem Description
A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.
The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
Input
Output
Sample Input
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0
Sample Output
1
5
2
4
Source
//2017-08-21
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
int head[N], tot;
struct Edge{
int to, next;
}edge[N<<]; void init(){
tot = ;
memset(head, -, sizeof(head));
} void add_edge(int u, int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} int n;
string G[N];
int matching[N];
int check[N]; bool dfs(int u){
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!check[v]){//要求不在交替路
check[v] = ;//放入交替路
if(matching[v] == - || dfs(matching[v])){
//如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功
matching[u] = v;
matching[v] = u;
return true;
}
}
}
return false;//不存在增广路
} //hungarian: 二分图最大匹配匈牙利算法
//input: null
//output: ans 最大匹配数
int hungarian(){
int ans = ;
memset(matching, -, sizeof(matching));
for(int u = ; u < n*n; u++){
if(matching[u] == -){
memset(check, , sizeof(check));
if(dfs(u))
ans++;
}
}
return ans;
} int main()
{
//freopen("inputA.txt", "r", stdin);
while(cin>>n && n){
init();
for(int i = ; i < n; i++)
cin>>G[i];
int id = , row_id[][], col_id[][];
memset(row_id, -, sizeof(row_id));
memset(col_id, -, sizeof(col_id));
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
if(G[i][j] == '.')row_id[i][j] = id;
else if(G[i][j] == 'X')id++;
}
id++;
}
id = n*n;
for(int j = ; j < n; j++){
for(int i = ; i < n; i++){
if(G[i][j] == '.')col_id[i][j] = id;
else if(G[i][j] == 'X')id++;
}
id++;
}
for(int i = ; i < n; i++)
for(int j = ; j < n; j++)
if(row_id[i][j] != - && col_id[i][j] != -)
add_edge(row_id[i][j], col_id[i][j]);
cout<<hungarian()<<endl;
} return ;
}
HDU1045(KB10-A 二分图最大匹配)的更多相关文章
- HDU1045 Fire Net —— 二分图最大匹配
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit: 2000/1000 MS (Java/Others) ...
- POJ 2226二分图最大匹配
匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图 ...
- POJ2239 Selecting Courses(二分图最大匹配)
题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 #include <iostream ...
- poj 2239 二分图最大匹配,基础题
1.poj 2239 Selecting Courses 二分图最大匹配问题 2.总结:看到一个题解,直接用三维数组做的,很巧妙,很暴力.. 题意:N种课,给出时间,每种课在星期几的第几节课上 ...
- UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法
二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...
- 二分图最大匹配的König定理及其证明
二分图最大匹配的K?nig定理及其证明 本文将是这一系列里最短的一篇,因为我只打算把K?nig定理证了,其它的废话一概没有. 以下五个问题我可能会在以后的文章里说,如果你现在很想知道的话,网上 ...
- POJ3057 Evacuation(二分图最大匹配)
人作X部:把门按时间拆点,作Y部:如果某人能在某个时间到达某门则连边.就是个二分图最大匹配. 时间可以二分枚举,或者直接从1枚举时间然后加新边在原来的基础上进行增广. 谨记:时间是个不可忽视的维度. ...
- ZOJ1654 Place the Robots(二分图最大匹配)
最大匹配也叫最大边独立集,就是无向图中能取出两两不相邻的边的最大集合. 二分图最大匹配可以用最大流来解. 如果题目没有墙,那就是一道经典的二分图最大匹配问题: 把地图上的行和列分别作为点的X部和Y部, ...
- HDU:过山车(二分图最大匹配)
http://acm.hdu.edu.cn/showproblem.php?pid=2063 题意:有m个男,n个女,和 k 条边,求有多少对男女可以搭配. 思路:裸的二分图最大匹配,匈牙利算法. 枚 ...
随机推荐
- Python基础 --函数的参数
定义函数的时候,我们把参数的名字和位置确定下来,函数的接口定义就完成了.对于函数的调用者来说,只需要知道如何传递正确的参数,以及函数将返回什么样的值就够了,函数内部的复杂逻辑被封装起来,调用者无需了解 ...
- 讲讲我当年是怎么拿到AI研发公司offer的
前言 很多的老铁私信问我,当年我是怎么拿到公司offer的,我记得我毕业是2015年,那时人工智能这个行业还没热起来,能提供的岗位很少但是面试的人更少,我又是本专业毕业的,所以当初找工作还算顺利,去面 ...
- SVM的基础原理
因为看cs231的时候用了一下multi-class的svm,所以又把svm给复习了一下,教材是周志华的西瓜书,这里是大概的笔记. 1.线性可分 对于一个数据集: 如果存在一个超平面X能够将D中的正负 ...
- graphite custom functions
尊重作者的劳动,转载请注明作者及原文地址 http://www.cnblogs.com/txwsqk/p/6522854.html 参考 https://graphite.readthedocs.io ...
- 如何做好错误处理?(PHP篇)
起因 之前我在封装 PHP 一个类库的时候,如果有遇到错误(例如构造函数传参不合法的话),则直接 die() ,后来发现这种方法很不好,会直接退出程序. 所以我想到给 PHP 上异常捕获的机制了. 错 ...
- postgresql-脏页和缓存失效
脏页和缓存失效 https://www.cnblogs.com/flying-tiger/p/7885478.html Dirty pages and cache invalidation 我们一直在 ...
- python并发编程之进程池,线程池concurrent.futures
进程池与线程池 在刚开始学多进程或多线程时,我们迫不及待地基于多进程或多线程实现并发的套接字通信,然而这种实现方式的致命缺陷是:服务的开启的进程数或线程数都会随着并发的客户端数目地增多而增多, 这会对 ...
- 安装SVN并进行汉化的详细步骤
安装SVN并进行汉化的详细步骤 SAE提供了不同的代码部署方式,可以分为两类:一是通过SVN客户端部署,这是SAE推荐的代码部署方法.另一个是通过非SVN客户端部署,即在线代码在线编辑器和推荐应用安装 ...
- Hadoop集群搭建中时间同步步骤
一.设置主节点时间服务器的时区 二.在每一个节点上检查是否安装时间服务ntp 三.在主节点上配置时间同步的相关文件 四.在其他从节点上配置与主节点时间同步的脚本 一.设 ...
- 德哥PostgreSQL学习资料汇总(转)
文章来自:https://yq.aliyun.com/articles/59251?spm=5176.100239.bloglist.95.5S5P9S 德哥博客新地址:https://billtia ...