POJ3020(最小边覆盖)

Antenna Placement

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8924   Accepted: 4428

Description

The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It is called 4DAir, and comes in four types. Each type can only transmit and receive signals in a direction aligned with a (slightly skewed) latitudinal and longitudinal grid, because of the interacting electromagnetic field of the earth. The four types correspond to antennas operating in the directions north, west, south, and east, respectively. Below is an example picture of places of interest, depicted by twelve small rings, and nine 4DAir antennas depicted by ellipses covering them. 
 
Obviously, it is desirable to use as few antennas as possible, but still provide coverage for each place of interest. We model the problem as follows: Let A be a rectangular matrix describing the surface of Sweden, where an entry of A either is a point of interest, which must be covered by at least one antenna, or empty space. Antennas can only be positioned at an entry in A. When an antenna is placed at row r and column c, this entry is considered covered, but also one of the neighbouring entries (c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on the type chosen for this particular antenna. What is the least number of antennas for which there exists a placement in A such that all points of interest are covered?

Input

On the first row of input is a single positive integer n, specifying the number of scenarios that follow. Each scenario begins with a row containing two positive integers h and w, with 1 <= h <= 40 and 0 < w <= 10. Thereafter is a matrix presented, describing the points of interest in Sweden in the form of h lines, each containing w characters from the set [‘*‘,‘o‘]. A ‘*‘-character symbolises a point of interest, whereas a ‘o‘-character represents open space.

Output

For each scenario, output the minimum number of antennas necessary to cover all ‘*‘-entries in the scenario‘s matrix, on a row of its own.

Sample Input

2
7 9
ooo**oooo
**oo*ooo*
o*oo**o**
ooooooooo
*******oo
o*o*oo*oo
*******oo
10 1
*
*
*
o
*
*
*
*
*
*

Sample Output

17
5思路:与POJ2446对比。若允许重叠则为最小边覆盖,否则为最大匹配。
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN=505;
int n,m;
vector<int> arc[MAXN];
char mz[MAXN][MAXN];
int vis[MAXN][MAXN];
int dy[4]={0,1,0,-1};
int dx[4]={1,0,-1,0};

int match[MAXN],used[MAXN];
bool dfs(int u)
{
    for(int i=0;i<arc[u].size();i++)
    {
        int to=arc[u][i];
        if(!used[to])
        {
            used[to]=1;
            int w=match[to];
            if(w==-1||dfs(w))
            {
                match[to]=u;
                match[u]=to;
                return true;
            }
        }
    }
    return false;
}
int max_flow()
{
    int ans=0;
    memset(match,-1,sizeof(match));
    int limit=n*m;
    for(int i=0;i<limit;i++)
    {
        if(match[i]==-1)
        {
            memset(used,0,sizeof(used));
            if(dfs(i))    ans++;
        }
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(vis,0,sizeof(vis));
        for(int i=0;i<MAXN;i++)    arc[i].clear();
        for(int i=0;i<n;i++)
        {
            scanf("%s",mz[i]);
        }
        int nodes=0;
        for(int y=0;y<n;y++)
        {
            for(int x=0;x<m;x++)
            {
                if(mz[y][x]!=‘*‘)    continue;
                nodes++;
                vis[y][x]=1;
                for(int i=0;i<4;i++)
                {
                    int ny=y+dy[i];
                    int nx=x+dx[i];
                    if(0<=ny&&ny<n&&0<=nx&&nx<m&&!vis[ny][nx]&&mz[ny][nx]==‘*‘)
                    {
                        int u=y*m+x;
                        int v=ny*m+nx;
                        arc[u].push_back(v);
                        arc[v].push_back(u);
                    }
                }
            }
        }
        int res=nodes-max_flow();
        printf("%d\n",res);
    }
    return 0;
}
				
时间: 2024-08-09 10:41:49

POJ3020(最小边覆盖)的相关文章

二分图之最小边覆盖(poj3020)

题目:poj3020 题意:给出一个图,让你用最少的1*2的纸片覆盖掉图中的所有*出现过的地方.基本裸的最小边覆盖. 分析: 最小边覆盖 = 点总数 - 最大匹配 所以就是转化为求最大匹配. 跟前面一道题目很相似,也是相同的建图方法,奇偶性建图. #include <cstdio> #include <cstring> #include <string> #include <iostream> #include <algorithm> #incl

[POJ3020]Antenna Placement(二分图最大匹配,最小边覆盖)

题目链接:http://poj.org/problem?id=3020 题意:要求用最少的1*2的边数覆盖掉图上的所有* 在专题里看这个图很高能所以不敢做,其实是最小边覆盖的裸题,先给*标号,随后按照每个*附近是否有*来构造二分图. 最后结果 最小边覆盖 = 节点数 - 最大匹配 (虽然和最小路径覆盖结果一样,但是是完全不同的两个事情). 最大匹配是这个图上匹配的1/2,因为建图的时候是双向的. 1 /* 2 ━━━━━┒ギリギリ♂ eye! 3 ┓┏┓┏┓┃キリキリ♂ mind! 4 ┛┗┛┗

POJ3020 Antenna Placement【二分图最小边覆盖】

题目链接: http://poj.org/problem?id=3020 题目大意: 在N*M的矩阵中,有K个城市要覆盖无线网.而一个无线网基站只能覆盖左右相邻或是上下相邻的两个 城市.问:至少放置多少个基站,能将这K个城市全部覆盖.输入数据时,'*'表示城市,'o'表示空地. 思路: K个城市作为K个点,编号为1~K.如果有两个城市相邻,则两个城市之间建立一条双向边.现在问题 变为了怎么从图中选择最少的边,使得能够覆盖所有的点.可以用二分图最小边覆盖来做.首先遍历 原图,对K个城市编号,存入i

二分图最大匹配,最小路径覆盖,最小点覆盖,最大独立集,最小边覆盖与建图方法

转载请注明出处(别管写的好坏,码字也不容易):http://blog.csdn.net/hitwhacmer1 前言:         有自己写的,有摘的别人的,前面是摘的,也是无心整理,出错是难免的,反正我都不会证明,智人见智,别被我误导了. §1图论点.边集和二分图的相关概念和性质 点覆盖.最小点覆盖 点覆盖集即一个点集,使得所有边至少有一个端点在集合里.或者说是"点" 覆盖了所有"边"..极小点覆盖(minimal vertex covering):本身为点覆

poj 3020Antenna Placement 最小边覆盖

//最小边覆盖 //最小边覆盖=最大独立集=n-最大匹配 //这个是在原图是二分图上进行的 //由于此题为无向图 //最小边覆盖=最大独立集=n-最大匹配/2; #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int maxn = 45*15; int line[maxn][maxn]; int match[maxn]; int vis[maxn]; cha

最小路径覆盖和最小边覆盖及相关性质

[最小路径覆盖] 首先给出公式:DAG的最小路径覆盖数=DAG图中的节点数-相应二分图中的最大匹配数. 一个PXP的有向图中,路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联:(如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次):如果不考虑图中存在回路,那么每条路径就是一个弱连通子集. 由上面可以得出: 1.一个单独的顶点是一条路径: 2.如果存在一路径p1,p2,......pk,其中p1 为起点,pk

J - Air Raid - hdu 1151(最小边覆盖)

题意:给一个有向无环图,求出来最少需要几个士兵可以遍历所有的边. 分析:有向无环图的最小边覆盖 = 点数 - 最大匹配数 为什么是这样的公式??可以思考一下,如果这N个点之间没有边,是不是应该有N个士兵去查看,但是如果增加一条边就应该减去这条边,以此类推,公式就比较容易明白了. ******************************************************************** #include<stdio.h>#include<string.h>

二分图中对最小顶点覆盖、最小边覆盖、最大独立集的理解[转]

原贴链接:http://blog.csdn.net/flynn_curry/article/details/52966283 仅仅用于自己理解,若有共鸣,别太吐槽就行哈~ 首先是匈牙利算法的本质:(图参考了zxy的) 这个图要详细看完,那么刚开始我想的“找小三”实际上就是递归找增广路的过程,如果找到增广路,匹配数就一定可以加一.(代码就不上了,都是一个模板) 理解到这里其实才只是个开始,我想解决的是最大匹配与最小顶点覆盖数.最小边覆盖数.最大点独立集之间的关系是怎么得来的.首先是结论: 在任意图

[转]最小路径覆盖和最小边覆盖及相关性质

转载自:http://www.cnblogs.com/icode-girl/p/5418461.html [最小路径覆盖] 首先给出公式:DAG的最小路径覆盖数=DAG图中的节点数-相应二分图中的最大匹配数. 一个PXP的有向图中,路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联:(如果把这些路径中的每条路径从它的起始点走到它的终点,那么恰好可以经过图中的每个顶点一次且仅一次):如果不考虑图中存在回路,那么每条路径就是一个弱连通子集. 由上面可以得出