您的位置:首页 > 其它

POJ 3020(最小路径覆盖)解题报告

2015-02-24 16:33 393 查看
Antenna Placement

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 6966Accepted: 3457
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

Source

Svenskt Mästerskap i Programmering/Norgesmesterskapet 2001

这道题是一道比较经典的最小路径覆盖问题。在说这道题之前,我们首先需要了解最小路径覆盖与最小顶点覆盖之间的区别。这些细微的差别必须搞清楚,否则会导致完全偏离题意。最小路径覆盖是用最少的边来覆盖所有的点,而最小顶点覆盖是用最少的顶点来保证所有的边都与这些顶点相连,即用最少的顶点来覆盖所有的边。这是两个完全不同的方向。而本题中,设立一个基站后,则这个基站可以与它周围相邻的四个点的任意一个点形成一个覆盖区域。而这种覆盖的关系就相当于是在这两个点之间进行连边。而我们需要关注的是要覆盖所有的‘*’。因此只需要考虑‘*’符号之间的关系即可。即该集合内所有点之间的关系,是否具有覆盖关系。而我们最终目的是用边来覆盖所有点,即用最少的边覆盖所有的点。这就顺理成章的与最小路径覆盖问题吻合。其中比较难想的是把这种覆盖四周相邻一点的特点理解为边。从而在‘*’点之间连边。

而本题最有疑问的是为何它是二分图最大匹配?首先我们需要清楚这个图是不是二分图,回答是。而本题使用的二分图建图方法就是拆点法。比如i点与j点间有一条边,而实质上是i与j拆出来的一个j'点相连。这样就出现了两个集合。而这实际上是一种构想。这一点也是非常巧妙的地方。把同一个集合拆分出两个集合进行二分图的最大匹配。最终由定理:最小路径覆盖数=顶点数-最大匹配数(如果是无向图,要除以2)。

参考代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#define pb push_back
#define CLR(x) memset(x,0,sizeof(x))
#define __CLR(x) memset(x,-1,sizeof(x))
using namespace std;

char a[50][20];
int b[50][20];
bool vis[500];
vector<int> G[500];
int match[500];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
int n,m;

bool dfs(int u)
{
    for(int i=0;i<G[u].size();i++)
    {
        int t=G[u][i];
        if(!vis[t])
        {
            vis[t]=1;
            if(match[t]==-1||dfs(match[t]))
            {
                match[t]=u;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        for(int i=1; i<=n; i++)
            scanf("%s",a[i]+1);
        CLR(b);
        int num=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]=='*')
                    b[i][j]=++num;
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(a[i][j]=='*')
                {
                    for(int k=0;k<4;k++)
                    {
                        int x1=i+dx[k],y1=j+dy[k];
                        if(x1>=1&&x1<=n&&y1>=1&&y1<=m)
                        {
                            if(a[x1][y1]=='*')
                            {
                                G[b[i][j]].pb(b[x1][y1]);
                                G[b[x1][y1]].pb(b[i][j]);
                            }
                        }
                    }
                }
            }
        }
        __CLR(match);
        int ans=0;
        for(int i=1;i<=num;i++)
        {
            CLR(vis);
            if(dfs(i))
                ans++;
        }
        printf("%d\n",num-ans/2);
        for(int i=0;i<500;i++)
            G[i].clear();
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: