题目地址:点击打开链接
题意:
给一个row*col的地图,城市记为’*’,空地记为'o', 现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可
以覆盖相邻的两个城市。问至少放置多少个基站才能使得所有的城市都覆盖无线?
思路:
每两个相邻的星号连一条边,很明显这是一个最小覆盖的问题,但是在构图过程中,匹配是双向的,即<u,v> 和<v,u>都算匹配,因此匹配数多了1倍,所以要除以2 。所以答案就是城市数-最大匹配/2
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxn = 405;
char pic[45][15];
int row, col, match[maxn], vis[maxn];
vector<int> g[maxn];
bool dfs(int x)
{
for(int i = 0; i < g[x].size(); i++)
{
int to = g[x][i];
if(!vis[to])
{
vis[to] = 1;
if(match[to] == -1 || dfs(match[to]))
{
match[to] = x;
return 1;
}
}
}
return 0;
}
int Hungary()
{
int cnt = 0;
for(int i = 1; i <= row*col; i++)
{
memset(vis, 0, sizeof(vis));
cnt += dfs(i);
}
return cnt;
}
int main(void)
{
int t;
cin >> t;
while(t--)
{
int cnt = 0;
memset(match, -1, sizeof(match));
for(int i = 0; i < maxn; i++)
g[i].clear();
scanf("%d%d", &row, &col);
for(int i = 1; i <= row; i++)
for(int j = 1; j <= col; j++)
{
scanf(" %c", &pic[i][j]);
if(pic[i][j] == '*')
{
cnt++;
if(pic[i-1][j] == '*')
{
g[(i-1)*col+j].push_back((i-2)*col+j);
g[(i-2)*col+j].push_back((i-1)*col+j);
}
if(pic[i][j-1] == '*')
{
g[(i-1)*col+j].push_back((i-1)*col+j-1);
g[(i-1)*col+j-1].push_back((i-1)*col+j);
}
}
}
int num = Hungary();
printf("%d\n", cnt-num/2);
}
return 0;
}
Antenna Placement
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 9417 | Accepted: 4650 |
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?
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