poj 3020 Antenna Placement 匈牙利算法

题目:

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

思路:

如果某个点为'*',则遍历看看它的四个方向上的元素如果也是'*',则在两个顶点建边。当然这需要对元素进行编号。

然后套用匈牙利算法。

题目的答案为最大匹配数加上没有匹配的顶点且为'*'的数目。

因为得出的结果ans为最大匹配数的两倍,所以答案为ans/2+(cnt-ans);(cnt为顶点为'*'的结点总数)。

这个题因为把左移运算符写成右移运算符搞错了,使数组缩小了好几倍,然后越界,调了好长时间。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=15;
int h,w,t;
char a[maxn<<2][maxn];
int id[maxn<<2][maxn];
int pos[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int cnt;
int head[maxn*maxn<<2];
int match[maxn*maxn<<3];
int vis[maxn*maxn<<3];
struct edge
{
    int to;
    int next;
}edge[maxn*maxn<<3];
void add (int id,int u,int v)
{
    edge[id].to=v;
    edge[id].next=head[u];
    head[u]=id;
}
bool Find(int x)
{
    for (int i=head[x];i!=-1;i=edge[i].next)
    {
        int u=edge[i].to;
        if(!vis[u])
        {
            vis[u]=1;
            if(match[u]==-1||Find(match[u]))
            {
                match[u]=x;
                return true;
            }
        }
    }
    return false;
}
int algor()
{
    int ans=0;
    memset (match,-1,sizeof(match));
    for (int i=1;i<=cnt;i++)
    {
        memset (vis,0,sizeof(vis));
        if(Find(i)) ans++;
    }
    return ans;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        memset (head,-1,sizeof(head));
        memset (id,0,sizeof(id));
        cnt=0;
        scanf("%d%d",&h,&w);
        getchar();
        int num=0;
        for (int i=0;i<h;i++)
        {
            for (int j=0;j<w;j++)
            {
                num++;
                scanf("%c",&a[i][j]);
                if(a[i][j]=='*')
                {
                    id[i][j]=++cnt;
                }
            }
            getchar();
        }
        for (int i=0,k=0;i<h;i++)
        {
            for (int j=0;j<w;j++)
            {
                if(a[i][j]=='*')
                {
                    for (int m=0;m<4;m++)
                    {
                        int tx=i+pos[m][0];
                        int ty=j+pos[m][1];
                        if(tx>=0&&tx<h&&ty>=0&&ty<w&&a[tx][ty]=='*')
                        {
                            add(++k,id[i][j],id[tx][ty]);
                        }
                    }
                }
            }
        }
        int ans=algor();
        printf("%d\n",cnt-ans/2);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/88362113