Antenna Placement poj 3020

Antenna Placement

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12104   Accepted: 5954

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

  1 // 题意:给一张图,图中有两种点,一次只能覆盖相邻(上下左右)两点
  2 // 求最小覆盖数
  3 // 二分图最小覆盖数等于最大匹配数,匈牙利算法求最大匹配数
  4 // 此题建图方面有些繁琐,看了大佬的题解后,才建图成功
  5
  6
  7 #include <cstdio>
  8 #include <cstring>
  9
 10 using namespace std;
 11
 12 const int max_h = 44;
 13 const int max_w = 14;
 14
 15 int h,w;
 16 char s[max_h][max_w];
 17
 18 int n;
 19 int total=0,save=0,cur;
 20 int direct[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
 21
 22 const int max_n=500+5;
 23 int cx[max_n],cy[max_n],st[max_n];
 24 bool vis[max_n];
 25
 26 struct node
 27 {
 28     int y,nxt;
 29 };
 30 // 这里数组开的小了点,但是刚好够用了,大佬在题解中开了比这大百倍的数组。不知道为什么
 31 // 这个数组的最大用量,应该由add函数的使用上限决定
 32 // 每遍历一个点,最多在上下左右四个方向进行一次add函数的调用,所以只需要4*max_n次即可
 33 node way[2000];
 34
 35 // 计算在一维数组中的位置
 36 int get(int i,int j)
 37 {
 38     return i*w+j;
 39 }
 40
 41 void add(int u,int v)
 42 {
 43     ++cur;
 44     // 当前数组中存储邻接点v和st【u】
 45     way[cur].y=v;
 46     way[cur].nxt=st[u];
 47     // st数组存储当u的边在数组中的位置
 48     st[u]=cur;
 49 }
 50
 51 int match(int x)
 52 {
 53     for(int i=st[x];i;i=way[i].nxt)
 54     {
 55         int y=way[i].y;
 56         if(!vis[y])
 57         {
 58             vis[y]=1;
 59             if(!cy[y] || match(cy[y]))
 60             {
 61                 cx[x]=y;
 62                 cy[y]=x;
 63                 return 1;
 64             }
 65         }
 66     }
 67     return 0;
 68 }
 69
 70 int XYL()
 71 {
 72     memset(cx,0,sizeof(cx));
 73     memset(cy,0,sizeof(cy));
 74     int ans=0;
 75     for(int i=0;i<n;++i)
 76     {
 77         // 遍历所有节点,如果找到没有匹配的x,看能否找到与之匹配的y
 78         // 这里加了一步判断,对st[i]不为0的判断,也就是对当前节点不为‘o‘的判断
 79         // 不加这一个判断也可得出正确的结果,但加入后会有优化,不用再执行之后许多无用的操作
 80         // 毕竟只有‘*‘的点需要匹配不是吗?
 81         if(!cx[i] && st[i])
 82         {
 83             memset(vis,0,sizeof(vis));
 84             ans+=match(i);
 85         }
 86     }
 87     return ans;
 88 }
 89
 90 int main()
 91 {
 92     int T;
 93     scanf("%d",&T);
 94     while(T--)
 95     {
 96         // 输入数据
 97         scanf("%d %d",&h,&w);
 98         for(int i=0;i<h;++i)
 99         {
100             scanf("%s",s[i]);
101         }
102
103         // 初始化way数组和st数组
104         cur=0;
105         memset(st,0,sizeof(st));
106         // n表示总节点数
107         n=h*w;
108         // total表示*的总数
109         total=0;
110
111
112         for(int i=0;i<h;++i)
113         {
114             for(int j=0;j<w;++j)
115             {
116                 if(s[i][j]==‘*‘)
117                 {
118                     ++ total;
119                     // 检查相邻四个方向
120                     for(int k=0;k<4;++k)
121                     {
122                         int tx=i+direct[k][0];
123                         int ty=j+direct[k][1];
124                         // 在图的范围内且为*时,加边
125                         if(tx>=0 && tx<h && ty>=0 && ty<w)
126                         {
127                             if(s[tx][ty]==‘*‘)
128                             {
129                                 int u=get(i,j);
130                                 int v=get(tx,ty);
131                                 add(u,v);
132                                 // printf("add");
133                             }
134                         }
135                     }
136                 }
137             }
138         }
139
140         //printf("cur:%d\n",tot);
141
142         printf("%d\n",total-XYL()/2);
143     }
144     return 0;
145 }
146
147 /*
148 2
149 7 9
150 ooo**oooo
151 **oo*ooo*
152 o*oo**o**
153 ooooooooo
154 *******oo
155 o*o*oo*oo
156 *******oo
157 10 1
158 *
159 *
160 *
161 o
162 *
163 *
164 *
165 *
166 *
167 *
168 */

原文地址:https://www.cnblogs.com/jishuren/p/12236404.html

时间: 2024-11-01 10:49:48

Antenna Placement poj 3020的相关文章

Antenna Placement POJ - 3020 (最小边集覆盖)

Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10699   Accepted: 5265 Description The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most s

Antenna Placement POJ - 3020 二分图匹配 匈牙利 拆点建图 最小路径覆盖

题意:图没什么用  给出一个地图 地图上有 点 一次可以覆盖2个连续 的点( 左右 或者 上下表示连续)问最少几条边可以使得每个点都被覆盖 最小路径覆盖       最小路径覆盖=|G|-最大匹配数                   证明:https://blog.csdn.net/qq_34564984/article/details/52778763 证明总的来说就是尽可能多得连边 边越多 可以打包一起处理得点就越多(这里题中打包指连续得两个点只需要一条线段就能覆盖) 拆点思想   :匈牙

poj 3020 Antenna Placement 解题报告

题目链接:http://poj.org/problem?id=3020 题目意思:首先,请忽略那幅有可能误导他人成分的截图(可能我悟性差,反正有一点点误导我了). 给出一幅 h * w 的图,  “ * ” 表示 point of interest,“ o ” 忽略之.你可以对 " * " (假设这个 “* ”的坐标是 (i, j))画圈,每个圈只能把它四周的某一个点括住(或者是上面(i-1, j) or 下面(i+1, j) or 左边(i, j-1)  or 右边(i, j+1))

POJ 3020 Antenna Placement ,二分图的最小路径覆盖

题目大意: 一个矩形中,有N个城市'*',现在这n个城市都要覆盖无线,若放置一个基站,那么它至多可以覆盖相邻的两个城市. 问至少放置多少个基站才能使得所有的城市都覆盖无线? 无向二分图的最小路径覆盖 = 顶点数 –  最大二分匹配数/2 路径覆盖就是在图中找一些路径,使之覆盖了图中的所有顶点,且任何一个顶点有且只有一条路径与之关联: #include<cstdio> #include<cstring> #include<vector> #include<algor

POJ 3020 Antenna Placement(二分图建图训练 + 最小路径覆盖)

题目链接:http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6692   Accepted: 3325 Description The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobi

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

Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6334   Accepted: 3125 Description The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most st

二分图匹配(匈牙利算法) POJ 3020 Antenna Placement

题目传送门 1 /* 2 题意:*的点占据后能顺带占据四个方向的一个*,问最少要占据多少个 3 匈牙利算法:按坐标奇偶性把*分为两个集合,那么除了匹配的其中一方是顺带占据外,其他都要占据 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <vector> 9 using namespace std; 10 11 const int MAXN = 4e

【POJ 3020】Antenna Placement

[POJ 3020]Antenna Placement 二分图的最大独立集问题 'o'表示间断点 要求把所有* 连接 每条路可连接一个或连续的两个* 最大匹配可以满足仅连接连续两个所能构成的最长路径 之后未被连接的点需要单独圈来套住 即n(总*数)-m(最大匹配)+m(最大匹配)/2 (由于建立的是双向路径 得到的最大匹配其实表示两两连接后连接的总点数) 代码如下: #include <cstdlib> #include <cstdio> #include <cstring&

poj 3020 Antenna Placement(最小路径覆盖 + 构图)

http://poj.org/problem?id=3020 Antenna Placement Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7565   Accepted: 3758 Description The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile ph