Antenna Placement(匈牙利算法 ,最少路径覆盖)

Antenna Placement

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6991   Accepted: 3466

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

无向二分图的最小路径覆盖 = 顶点数 – 最大二分匹配数/2 ;

详情,点这里

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<queue>
 4 #include<string.h>
 5 using namespace std;
 6 bool map[500][500] ;
 7 int vis[500][500] ;
 8 int a[500][500] ;
 9 int girl [500] ;
10 bool sta[500] ;
11 int cnt ;
12 int row , col ;
13 char st[500] ;
14 int move[][2] = {1 , 0 , 0 , 1 , -1 , 0 , 0 , -1} ;
15
16 bool hungary (int x)
17 {
18     for (int i = 1 ; i <= cnt ; i++) {
19         if (map[x][i] && sta[i] == false) {
20             sta[i] = true ;
21             if (girl[i] == 0 || hungary (girl[i])) {
22                 girl[i] = x ;
23                 return true ;
24             }
25         }
26     }
27     return false ;
28 }
29
30 int main ()
31 {
32    // freopen ("a.txt" , "r" , stdin) ;
33     int T ;
34     cin >> T ;
35     while (T--) {
36         scanf ("%d%d" , &row , &col) ;
37         getchar () ;
38         cnt = 0 ;
39         memset (vis , -1 , sizeof(vis)) ;
40         memset (map , 0 , sizeof(map)) ;
41         memset (a , -1 , sizeof(a)) ;
42         memset (girl , 0 , sizeof(girl)) ;
43         for (int i = 0 ; i < row ; i++) {
44             gets (st) ;
45             for (int j = 0 ; j < col ; j++) {
46                if (st[j] == ‘*‘) {
47                     a[i + 1][j + 1] = 1 + cnt++;
48                }
49             }
50         }
51        /* for (int i = 1 ; i <= row ; i++) {
52             for (int j = 1 ; j <= col ; j++) {
53                 printf ("%d " , a[i][j]);
54             }
55             puts ("") ;
56         }*/
57         for (int i = 1 ; i <= row ; i++) {
58             for (int j = 1 ; j <= col ; j++) {
59                 if (a[i][j] != -1) {
60                     for (int k = 0 ; k < 4 ; k++) {
61                         int x = i + move[k][0] ;
62                         int y = j + move[k][1] ;
63                         if (a[x] [y] != -1)
64                             map[ a[i][j] ] [ a[x][y] ] = 1 ;
65                     }
66                 }
67             }
68         }
69        /* for (int i = 1 ; i <= cnt ; i++) {
70             for (int j = 1 ; j <= cnt ; j++) {
71                 printf ("%d " , map[i][j]) ;
72             }
73             puts ("") ;
74         }*/
75         int all = 0 ;
76         for (int i = 1 ; i <= cnt ; i++) {
77             memset (sta , 0 , sizeof(sta)) ;
78             if (hungary (i))
79                 all ++ ;
80         }
81       //  printf ("cnt = %d , all = %d\n" , cnt , all) ;
82         printf ("%d\n" , cnt - all / 2) ;
83     }
84     return 0 ;
85 }

时间: 2024-10-25 19:06:04

Antenna Placement(匈牙利算法 ,最少路径覆盖)的相关文章

poj3020 Antenna Placement 匈牙利算法求最小覆盖=最大匹配数(自身对应自身情况下要对半) 小圈圈圈点

/** 题目:poj3020 Antenna Placement 链接:http://poj.org/problem?id=3020 题意: 给一个由'*'或者'o'组成的n*m大小的图,你可以用一个小圈圈圈住两个相邻的'*',问要圈住所有的'*'最少需要多少个小圈圈.(小圈圈可以相交) 思路: 先尽量圈出能圈两个且不重复圈的'*'.剩下的没有圈的'*'一定需要用一个. 所以构造二分图,求最大匹配,结果:ans = 总的'*'数量-最大匹配数*2 + 最大匹配数 = 总的'*'数量-最大匹配数:

poj 1422 Air Raid 最少路径覆盖

题目链接:http://poj.org/problem?id=1422 Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an intersection and walking through town's streets you can never reach t

关于最大匹配,最小点覆盖,最少路径覆盖和最大独立集的总结

(1)二分图的最大匹配 匈牙利算法 (2)二分图的最小点覆盖 二分图的最小点覆盖=二分图的最大匹配 求最小点覆盖:从右边所有没有匹配过的点出发,按照增广路的“交替出现”的要求DFS.最终右边没有访问过的点和左边访问过的点组成最小点覆盖. 证明见这里 (3)二分图的最少边覆盖 二分图的最少边覆盖=点数-二分图的最大匹配 证明: 先贪心选一组最大匹配的边放进集合,对于剩下的没有匹配的点,随便选一条与之关联的边放进集合,那么得到的集合就是最小边覆盖. 所以有:最小边覆盖=最大匹配+点数-2*最大匹配=

匈牙利算法实战codevs1022覆盖

1022 覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 有一个N×M的单位方格中,其中有些方格是水塘,其他方格是陆地.如果要用1×2的矩阵区覆盖(覆盖过程不容许有任何部分重叠)这个陆地,那么最多可以覆盖多少陆地面积. 输入描述 Input Description 输入文件的第一行是两个整数N,M  (1<=N,M<=100),第二行为一个整数K( K<=50),接下来的K行,每行两个整数X

POJ-3020 Antenna Placement---二分图匹配&amp;最小路径覆盖&amp;建图

题目链接: https://vjudge.net/problem/POJ-3020 题目大意: 一个n*m的方阵 一个雷达可覆盖两个*,一个*可与四周的一个*被覆盖,一个*可被多个雷达覆盖问至少需要多少雷达能把所有的*覆盖 解题思路: 把每个*城市编号,然后每相邻两个城市之间连线.这里求最少多少个雷达可以覆盖完*,就是二分图匹配中的最小路径覆盖数,但是这里的图的边是双向的.举个例子 o*o **o ooo 这里可以编号成 010 230 000 那么有边<1,3><3,1><

二分图的最大匹配:匈牙利算法

1. 二分图的匹配问题 1.1 二分图 简单来说,如果图中点可以被分为两组,并且使得所有边都跨越组的边界,则这就是一个二分图. 准确地说:把一个图的顶点划分为两个不相交集 U 和 V ,使得每一条边都分别连接U . V 中的顶点.如果存在这样的划分,则此图为一个二分图. 二分图的一个等价定义是:不含有「含奇数条边的环」的图.图 1 是一个二分图.为了清晰,我们以后都把它画成图 2 的形式. 1.2 匹配 在图论中,一个「匹配」(matching)是一个边的集合,其中任意两条边都没有公共顶点.例如

HDU - 6311 Cover(无向图的最少路径边覆盖 欧拉路径)

题意 给个无向图,无重边和自环,问最少需要多少路径把边覆盖了.并输出相应路径 分析 首先联通块之间是独立的,对于一个联通块内,最少路径覆盖就是  max(1,度数为奇数点的个数/2).然后就是求欧拉路径了,先将块内度数为奇数的点找出来,留下两个点,其余两两连上虚边,这样我们选择从一个奇数点出发到另一个奇数点,求出一条欧拉路径,统计总路径数.接着就dfs,注意一些细节. 附赠一个求欧拉回路的fleury算法:https://blog.csdn.net/u011466175/article/deta

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

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

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