二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement
/*
题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个
匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std; const int MAXN = 4e2 + ;
const int INF = 0x3f3f3f3f;
char s[][];
int ha[][];
bool vis[MAXN];
int lk[MAXN];
vector<int> G[MAXN];
int dx[] = {-, , , };
int dy[] = {, , -, };
int un, vn; bool DFS(int u)
{
for (int i=; i<G[u].size (); ++i)
{
int v = G[u][i];
if (!vis[v])
{
vis[v] = true;
if (lk[v] == - || DFS (lk[v]))
{
lk[v] = u; return true;
}
}
} return false;
} int hungary(void)
{
int res = ; memset (lk, -, sizeof (lk));
for (int i=; i<=un; ++i)
{
memset (vis, false, sizeof (vis));
if (DFS (i)) res++;
} return res;
} int main(void) //POJ 3020 Antenna Placement
{
//freopen ("POJ_3020.in", "r", stdin); int t; scanf ("%d", &t);
while (t--)
{
int h, w; scanf ("%d%d", &h, &w);
for (int i=; i<=h; ++i)
{
scanf ("%s", s[i] + );
} un = vn = ;
for (int i=; i<=h; ++i)
{
for (int j=; j<=w; ++j)
{
if (s[i][j] == '*')
{
if ((i+j) & ) ha[i][j] = ++un;
else ha[i][j] = ++vn;
}
}
} for (int i=; i<=un; ++i) G[i].clear (); for (int i=; i<=h; ++i)
{
for (int j=; j<=w; ++j)
{
if (s[i][j] == '*' && (i+j) & )
{
for (int k=; k<; ++k)
{
int tx = i + dx[k]; int ty = j + dy[k];
if (tx >= && tx <= h && ty >= && ty <= w && s[tx][ty] == '*')
G[ha[i][j]].push_back (ha[tx][ty]);
}
}
}
} printf ("%d\n", un + vn - hungary ());
} return ;
}
二分图最大匹配(匈牙利算法) POJ 3020 Antenna Placement的更多相关文章
- 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids
题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...
- poj 3020 Antenna Placement(最小路径覆盖 + 构图)
http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ 3020 Antenna Placement 【最小边覆盖】
传送门:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total ...
- POJ 3020 Antenna Placement(二分图 匈牙利算法)
题目网址: http://poj.org/problem?id=3020 题意: 用椭圆形去覆盖给出所有环(即图上的小圆点),有两种类型的椭圆形,左右朝向和上下朝向的,一个椭圆形最多可以覆盖相邻的两 ...
- poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)
http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...
- poj 3020 Antenna Placement(二分无向图 匈牙利)
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6438 Accepted: 3176 ...
- POJ 3020 Antenna Placement 最大匹配
Antenna Placement Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6445 Accepted: 3182 ...
- poj 3894 System Engineer (二分图最大匹配--匈牙利算法)
System Engineer Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 507 Accepted: 217 Des ...
- POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- Github上600多个iOS开源项目分类及介绍
将Github上600多个iOS开源项目进行分类并且有相应介绍,小伙伴们快来看呀 地址:http://github.ibireme.com/github/list/ios/
- BZOJ2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
n<=250个大写字母和m<=250个小写字母,给p<=200个合法相邻字母,求用这些合法相邻字母的规则和n+m个字母能合成多少合法串,答案mod 97654321. 什么鬼膜数.. ...
- 使用MediaPlayer播放、暂停、停止音乐
package com.pingyijinren.test; import android.media.MediaPlayer; import android.os.Environment; impo ...
- Devu and Flowers lucas定理+容斥原理
Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box contain ...
- React Native学习(十)—— 生命周期
本文基于React Native 0.52 Demo上传到Git了,有需要可以看看,写了新内容会上传的.Git地址 https://github.com/gingerJY/React-Native-D ...
- Use Elasticksearch to solve TOP N issue
The raw data is like timestamp, router, interface, src_ip, dst_ip, protocol, pkts 10000000, 1.1.1.1 ...
- mysql导入大型sql文件时注意事项
原文:http://blog.csdn.net/k21325/article/details/70808563 大型sql文件,需要在my.ini(windows)或者my.cnf(Linux)中设置 ...
- symfony 参考
多语言存数据库 http://blog.elendev.com/development/php/symfony/use-a-database-as-translation-provider-in-sy ...
- CF# 368 D2 D
很容易想到可以它操作序列弄成有向图,果断深搜.但我开始竟然用了一种特醇的方法,每个书架做一次深搜,复杂度O(nq),跑到57个test就不动了.看看代码吧 #include <iostream& ...
- Python学习系列之面向对象
概述 一.Python编程方式 面向过程编程:根据业务逻辑从上到下磊代码 面向函数编程:将某功能代码封装到函数中,将来直接调用即可,无需重新写 面向对象编程:对函数进行分类.封装 二.面向过程编程 w ...