首页 > 代码库 > POJ 3020 Antenna Placement(二分图建图训练 + 最小路径覆盖)
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 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.
题意:给定一个地图,*代表城市,o代表空地,用天线来覆盖相邻的两个城市,问最少需要多少天线?(所谓相邻是指上下左右4个方向相邻)
题意很清晰,求二分图的最小路径覆盖,难点还是建图。。。今天运气好,建图的思路来源于ZOJ1654
PS,覆盖的是城市,不是空地,开始我就数错了
思路:刚好今天做了ZOJ 1654,和1654差不多,那个采用分块的思想,而这个是把单个的城市当做一块,进行编号从而构建图的连通性,至于最后的输出结果,城市总数减去最大匹配数==剩余的城市数,也就是最大独立集合数,剩余城市的个数说明他们无法进行增广/匹配,那么就需要单独建设天线,而 匹配数/2 == 一个天线覆盖两个城市
所以最终answer = city - ans + ans/2
ZOJ 1654 AC代码
Accepted | 1584K | 16MS | C++ |
想明白后,直接开敲,手残一次,1A
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <math.h> #define init(a) memset(a,0,sizeof(a)) #define PI acos(-1,0) using namespace std; const int maxn = 60; const int maxm = 600; #define lson left, m, id<<1 #define rson m+1, right, id<<1|1 #define min(a,b) (a>b)?b:a #define max(a,b) (a>b)?a:b int ma[maxn][maxn]; char MAP[maxn][maxn]; int G[maxm][maxm]; int line[maxm]; bool vis[maxm]; int mv[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; int n,m,city; int DFS(int u) { for(int v = 1;v<=city;v++) { if(G[u][v]==1 && !vis[v]) { vis[v] = 1; if(line[v]==-1 || DFS(line[v])) { line[v] = u; return 1; } } } return 0; } int K_M() { int ans = 0; memset(line,-1,sizeof(line)); for(int i = 1;i<=city;i++) { init(vis); ans += DFS(i); } return ans; } void Get_G(int i,int j)//构图 { for(int dir = 0;dir<4;dir++) { int wx = i + mv[dir][0]; int wy = j + mv[dir][1]; if(MAP[wx][wy]=='*') { G[ma[i][j]][ma[wx][wy]] = 1;//构建当前城市与它4个方向相邻的城市连通 } } } int main() { int t; scanf("%d",&t); char a[500]; while(t--) { scanf("%d%d",&n,&m); city = 0; gets(a);//不加,测试数据就读不进。。。。。我特别无语 init(ma); init(G); for(int i = 0;i<n;i++) { gets(MAP[i]); for(int j = 0;j<m;j++) { if(MAP[i][j]=='*') ma[i][j] = ++city;//给所有城市编号 } } for(int i = 0;i<n;i++) { for(int j = 0;j<m;j++) { if(MAP[i][j]=='*') { Get_G(i,j);//构建二分图 } } } int ans = K_M(); // printf("cisy = %d ans = %d\n",city,ans); //一个匹配相当于一个点被覆盖 printf("%d\n",city-ans+ans/2); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。