题目链接:

https://vjudge.net/problem/POJ-3020

题目大意:

一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多少雷达能把所有的*覆盖

解题思路:

把每个*城市编号,然后每相邻两个城市之间连线。这里求最少多少个雷达可以覆盖完*,就是二分图匹配中的最小路径覆盖数,但是这里的图的边是双向的。举个例子

o*o

**o

ooo

这里可以编号成

010

230

000

那么有边<1,3><3,1><2,3><3,2>

按照二分图匹配建图的方法,每个点拆分成两个点A1,A2,如果有边<A, B>在二分图中建立边<A1, B2>。

这里的特殊性在于1和3有边,3和1也有边,所以最后求出来的最大匹配需要除以2才是题目所需要的最大匹配

最小路径覆盖数 = 顶点数 - 最大匹配

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int, int> Pair ;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn = + ;
int T, n, m, cases;
vector<int>G[maxn];
int cx[maxn], cy[maxn];
bool vis[maxn];
char Map[][];
int cnt[][], tot;
int dir[][] = {,,,,-,,,-};
bool dfs(int u)
{
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(!vis[v])
{
vis[v] =;//加入增广路
if(cy[v] == - || dfs(cy[v]))
{
cx[u] = v;
cy[v] = u;
return ;
}
}
}
return ;
} int maxmatch()
{
int ans = ;
memset(cx, -, sizeof(cx));
memset(cy, -, sizeof(cy));
for(int i = ; i <= tot; i++)
{
if(cx[i] == -)
{
memset(vis, , sizeof(vis));
ans += dfs(i);
}
}
return ans;
} int main()
{
cin >> T;
while(T--)
{
scanf("%d%d", &n, &m);
tot = ;
for(int i = ; i < maxn; i++)G[i].clear();
for(int i = ; i < n; i++)//将Map转化成城市的编号
{
cin >> Map[i];
for(int j = ; j < m ;j++)
if(Map[i][j] == 'o')cnt[i][j] = ;
else cnt[i][j] = ++tot;
}/*
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)cout<<cnt[i][j]<<" ";
cout<<endl;
}*/
for(int i = ; i < n; i++)
//二分图建图,每个点拆成两个点,建成有向图,并且每两点之间有两条相反边,所以求出来的最大匹配是真正匹配的两倍
{
for(int j = ; j < m; j++)
{
if(!cnt[i][j])continue;
for(int k = ; k < ; k++)
{
int xx = i + dir[k][];
int yy = j + dir[k][];
if(!cnt[xx][yy])continue;
if(xx < || xx >= n || yy < || yy >= m)continue;
int u = cnt[i][j], v = cnt[xx][yy];
//cout<<u<<" "<<v<<endl;
G[u].push_back(v);
}
}
}
//最小路径覆盖数 = 顶点数 - 最大匹配数
int ans = tot - maxmatch() / ;
cout<<ans<<endl;
}
return ;
}

POJ-3020 Antenna Placement---二分图匹配&最小路径覆盖&建图的更多相关文章

  1. POJ 3020 Antenna Placement (二分图最小路径覆盖)

    <题目链接> 题目大意:一个矩形中,有N个城市’*’,现在这n个城市都要覆盖无线,每放置一个基站,至多可以覆盖相邻的两个城市.问至少放置多少个基站才能使得所有的城市都覆盖无线? 解题分析: ...

  2. POJ 1422 Air Raid(二分图匹配最小路径覆盖)

    POJ 1422 Air Raid 题目链接 题意:给定一个有向图,在这个图上的某些点上放伞兵,能够使伞兵能够走到图上全部的点.且每一个点仅仅被一个伞兵走一次.问至少放多少伞兵 思路:二分图的最小路径 ...

  3. UVA 1201 - Taxi Cab Scheme(二分图匹配+最小路径覆盖)

    UVA 1201 - Taxi Cab Scheme 题目链接 题意:给定一些乘客.每一个乘客须要一个出租车,有一个起始时刻,起点,终点,行走路程为曼哈顿距离,每辆出租车必须在乘客一分钟之前到达.问最 ...

  4. POJ - 3020  Antenna Placement 二分图最大匹配

    http://poj.org/problem?id=3020 首先注意到,答案的最大值是'*'的个数,也就是相当于我每用一次那个技能,我只套一个'*',是等价的. 所以,每结合一对**,则可以减少一次 ...

  5. POJ 3020 Antenna Placement(二分图 匈牙利算法)

    题目网址:  http://poj.org/problem?id=3020 题意: 用椭圆形去覆盖给出所有环(即图上的小圆点),有两种类型的椭圆形,左右朝向和上下朝向的,一个椭圆形最多可以覆盖相邻的两 ...

  6. [POJ] 3020 Antenna Placement(二分图最大匹配)

    题目地址:http://poj.org/problem?id=3020 输入一个字符矩阵,'*'可行,'o'不可行.因为一个点可以和上下左右四个方向的一个可行点组成一个集合,所以对图进行黑白染色(每个 ...

  7. POJ3020 二分图匹配——最小路径覆盖

    Description The Global Aerial Research Centre has been allotted the task of building the fifth gener ...

  8. POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】

    链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  9. POJ 3020:Antenna Placement(无向二分图的最小路径覆盖)

    Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6334   Accepted: 3125 ...

随机推荐

  1. maven set MAVEN_OPTS

    http://juvenshun.iteye.com/blog/240257 https://docs.alfresco.com/5.1/tasks/alfresco-sdk-install-mave ...

  2. mybatis-Plus 增强版用法收藏

    转载:http://www.cnblogs.com/okong/p/mybatis-plus-guide-one.html#xml%E5%BD%A2%E5%BC%8F https://blog.csd ...

  3. vue(4)hello world

    在前一章基础上开发. 1.下载vue.js.(https://cn.vuejs.org/v2/guide/installation.html) 在hello-vue根目录下创建js文件夹,并将该vue ...

  4. ios中页面底部输入框,position:fixed元素的问题

    在安卓上点击页面底部的输入框,软键盘弹出,页面移动上移.ios上,软件盘弹出,输入框看不到了.让他弹出时让滚动条在最低部 var u = navigator.userAgent, app = navi ...

  5. Subarray Sum K

    Given an nonnegative integer array, find a subarray where the sum of numbers is k. Your code should ...

  6. 配置编译器(GCC和GFortran)

    平台信息 Description: CentOS Linux release 7.6.1810 (Core) 检查环境 $ gfortran -v $ gcc -v 安装 GCC和Fortran 环境 ...

  7. XGBoost算法

    一.基础知识 (1)泰勒公式 泰勒公式是一个用函数在某点的信息描述其附近取值的公式.具有局部有效性. 基本形式如下: 由以上的基本形式可知泰勒公式的迭代形式为: 以上这个迭代形式是针对二阶泰勒展开,你 ...

  8. php防止重复提交问题总结

    用户提交表单时可能因为网速的原因,或者网页被恶意刷新,致使同一条记录重复插入到数据库中,这是一个比较棘手的问题.我们可以从客户端和服务器端一起着手,设法避免同一表单的重复提交. 1.使用客户端脚本 提 ...

  9. MakeFile基本使用

    MakeFile Making makefile demo # Run this line when useing `make` command # default is the target whi ...

  10. Powershell(2)

    powershell(2) 帮助系统(显示出来的参数语法要比Unix中help复杂) save-help -destinationpath c:\help update-help -force -so ...