二分图匹配(匈牙利算法) 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 = 4e2 + 10;
 12 const int INF = 0x3f3f3f3f;
 13 char s[44][11];
 14 int ha[44][11];
 15 bool vis[MAXN];
 16 int lk[MAXN];
 17 vector<int> G[MAXN];
 18 int dx[4] = {-1, 1, 0, 0};
 19 int dy[4] = {0, 0, -1, 1};
 20 int un, vn;
 21
 22 bool DFS(int u)
 23 {
 24     for (int i=0; i<G[u].size (); ++i)
 25     {
 26         int v = G[u][i];
 27         if (!vis[v])
 28         {
 29             vis[v] = true;
 30             if (lk[v] == -1 || DFS (lk[v]))
 31             {
 32                 lk[v] = u;  return true;
 33             }
 34         }
 35     }
 36
 37     return false;
 38 }
 39
 40 int hungary(void)
 41 {
 42     int res = 0;    memset (lk, -1, sizeof (lk));
 43     for (int i=1; i<=un; ++i)
 44     {
 45         memset (vis, false, sizeof (vis));
 46         if (DFS (i))    res++;
 47     }
 48
 49     return res;
 50 }
 51
 52 int main(void)        //POJ 3020 Antenna Placement
 53 {
 54     //freopen ("POJ_3020.in", "r", stdin);
 55
 56     int t;  scanf ("%d", &t);
 57     while (t--)
 58     {
 59         int h, w;   scanf ("%d%d", &h, &w);
 60         for (int i=1; i<=h; ++i)
 61         {
 62             scanf ("%s", s[i] + 1);
 63         }
 64
 65         un = vn = 0;
 66         for (int i=1; i<=h; ++i)
 67         {
 68             for (int j=1; j<=w; ++j)
 69             {
 70                 if (s[i][j] == ‘*‘)
 71                 {
 72                     if ((i+j) & 1)  ha[i][j] = ++un;
 73                     else    ha[i][j] = ++vn;
 74                 }
 75             }
 76         }
 77
 78         for (int i=1; i<=un; ++i)   G[i].clear ();
 79
 80         for (int i=1; i<=h; ++i)
 81         {
 82             for (int j=1; j<=w; ++j)
 83             {
 84                 if (s[i][j] == ‘*‘ && (i+j) & 1)
 85                 {
 86                     for (int k=0; k<4; ++k)
 87                     {
 88                         int tx = i + dx[k]; int ty = j + dy[k];
 89                         if (tx >= 1 && tx <= h && ty >= 1 && ty <= w && s[tx][ty] == ‘*‘)
 90                             G[ha[i][j]].push_back (ha[tx][ty]);
 91                     }
 92                 }
 93             }
 94         }
 95
 96         printf ("%d\n", un + vn - hungary ());
 97     }
 98
 99     return 0;
100 }
时间: 2024-08-01 19:29:27

二分图匹配(匈牙利算法) POJ 3020 Antenna Placement的相关文章

二分图匹配(匈牙利算法) POJ 3041 Asteroids

题目传送门 1 /* 2 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 3 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 4 趣味入门:http://blog.csdn.net/dark_scope/article/details/8880547 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include

POJ 3020 Antenna Placement(二分图匹配)

POJ 3020 Antenna Placement 题目链接 题意:给定一个地图,'*'的地方要被覆盖上,每次可以用1 x 2的矩形去覆盖,问最少用几个能覆盖 思路:二分图匹配求最大独立集,相邻*之间连边,然后求最大独立集即可 看这数据范围,用轮廓线dp应该也能搞 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace s

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 解题报告

题目链接: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))

USACO 4.2 The Perfect Stall(二分图匹配匈牙利算法)

The Perfect StallHal Burch Farmer John completed his new barn just last week, complete with all the latest milking technology. Unfortunately, due to engineering problems, all the stalls in the new barn are different. For the first week, Farmer John r

HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very

HDU1507 Uncle Tom&#39;s Inherited Land* 二分图匹配 匈牙利算法 黑白染色

原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1*2的矩形覆盖所有的不废的点,并且不重叠,问最多可以覆盖多少个1*2的矩形,输出方案,有SPJ. 输入描述: 多组数据,每组首先两个数n,m(如果n和m为0,则结束程序) 然后给出k 然后给出k个二元组(x,y)表示废点的坐标. 题解 按照前两片博文的算法已经不行了,因为方案不对了. 所以我们要进行

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