标签:been cep build minimum \n math placed return task
传送门:http://poj.org/problem?id=3020
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11098 | Accepted: 5464 |
Description
Input
Output
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
给一个高为 H 宽为 W 的图案, “ * ” 表示城市,每个城市可以覆盖 它 上下左右相邻的 其中一个城市,问最好需要多少城市才能把所有城市覆盖。
按照城市编号,然后拆点建图,相邻两个城市可以覆盖的就连边,跑一遍最小边覆盖。
AC code:
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <cmath> 5 #define INF 0x3f3f3f3f 6 using namespace std; 7 const int MAXH = 45; 8 const int MAXW = 15; 9 const int MAXN = 500; 10 struct Edge 11 { 12 int v, nxt; 13 }edge[MAXN*MAXN]; 14 int head[MAXN], cnt; 15 int linker[MAXN]; 16 bool used[MAXN]; 17 char str[MAXH][MAXW]; 18 int a[MAXH][MAXW]; 19 int sum; 20 int H, W; 21 22 void init() 23 { 24 memset(head, -1, sizeof(head)); 25 memset(linker, -1, sizeof(linker)); 26 memset(a, 0, sizeof(a)); 27 cnt = 0; 28 sum = 0; 29 } 30 31 void add(int from, int to) 32 { 33 edge[cnt].v = to; 34 edge[cnt].nxt = head[from]; 35 head[from] = cnt++; 36 } 37 38 bool Find(int x) 39 { 40 int v; 41 for(int i = head[x]; i != -1; i = edge[i].nxt){ 42 v = edge[i].v; 43 if(!used[v]){ 44 used[v] = true; 45 if(linker[v] == -1 || Find(linker[v])){ 46 linker[v] = x; 47 return true; 48 } 49 } 50 } 51 return false; 52 } 53 54 int main() 55 { 56 int T_case; 57 scanf("%d", &T_case); 58 while(T_case--){ 59 init(); 60 scanf("%d%d", &H, &W); 61 for(int i = 0; i < H; i++){ 62 scanf("%s", &str[i]); 63 for(int j = 0; j < W; j++) 64 if(str[i][j]==‘*‘) a[i][j] = ++sum; 65 } 66 67 for(int i = 0; i < H; i++){ 68 for(int j = 0; j < W; j++){ 69 if(a[i][j]){ 70 if(i > 0 && a[i-1][j]) add(a[i][j], a[i-1][j]); 71 if(i < H-1 && a[i+1][j]) add(a[i][j], a[i+1][j]); 72 if(j > 0 && a[i][j-1]) add(a[i][j], a[i][j-1]); 73 if(j < W-1 && a[i][j+1]) add(a[i][j], a[i][j+1]); 74 } 75 } 76 } 77 78 int ans = 0; 79 for(int i = 1; i <= sum; i++){ 80 memset(used, 0, sizeof(used)); 81 if(Find(i)) ans++; 82 } 83 printf("%d\n", sum-ans/2); 84 } 85 return 0; 86 }
POJ 3020 Antenna Placement 【最小边覆盖】
标签:been cep build minimum \n math placed return task
原文地址:https://www.cnblogs.com/ymzjj/p/10001332.html