PKU 2226 - Muddy Fields (最小点覆盖)

题意

用最少的木板覆盖。

思路

很有意思的建图。横着标号,然后竖着标号,建边,求最小点覆盖。

代码

  
  
  
  
  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <string>
  13. #include <map>
  14. #include <cmath>
  15. #define LL long long
  16. #define SZ(x) (int)x.size()
  17. #define Lowbit(x) ((x) & (-x))
  18. #define MP(a, b) make_pair(a, b)
  19. #define MS(arr, num) memset(arr, num, sizeof(arr))
  20. #define PB push_back
  21. #define F first
  22. #define S second
  23. #define ROP freopen("input.txt", "r", stdin);
  24. #define MID(a, b) (a + ((b - a) >> 1))
  25. #define LC rt << 1, l, mid
  26. #define RC rt << 1|1, mid + 1, r
  27. #define LRT rt << 1
  28. #define RRT rt << 1|1
  29. #define BitCount(x) __builtin_popcount(x)
  30. const double PI = acos(-1.0);
  31. const int INF = 0x3f3f3f3f;
  32. using namespace std;
  33. const int MAXN = 2000 + 10;
  34. const int MOD = 1e9 + 7;
  35. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  36. typedef pair<int, int> pii;
  37. typedef vector<int>::iterator viti;
  38. typedef vector<pii>::iterator vitii;
  39. struct EDGE
  40. {
  41. int from, to, cap;
  42. };
  43. struct HUNGARY
  44. {
  45. int vis[MAXN], link[MAXN], head[MAXN], next[MAXN * MAXN];
  46. vector<EDGE> edges;
  47. void init()
  48. {
  49. edges.clear();
  50. MS(head, -1); MS(link, -1);
  51. }
  52. int solve(int nrem)
  53. {
  54. int res = 0;
  55. for (int i = 1; i <= nrem; i++)
  56. {
  57. MS(vis, 0);
  58. res += DFS(i);
  59. }
  60. return res;
  61. }
  62. void add_edge(int from, int to)
  63. {
  64. EDGE a = {from, to};
  65. edges.PB(a);
  66. int m = SZ(edges);
  67. next[m - 1] = head[from];
  68. head[from] = m - 1;
  69. }
  70. int DFS(int u)
  71. {
  72. for (int i = head[u]; i != -1; i = next[i])
  73. {
  74. int v = edges[i].to;
  75. if (!vis[v])
  76. {
  77. vis[v] = 1;
  78. if (link[v] == -1 || DFS(link[v]))
  79. {
  80. link[v] = u;
  81. return 1;
  82. }
  83. }
  84. }
  85. return 0;
  86. }
  87. }hun;
  88. char mp[MAXN][MAXN];
  89. int num[MAXN][MAXN];
  90. int main()
  91. {
  92. //ROP;
  93. int row, col, i, j;
  94. while (~scanf("%d%d", &row, &col))
  95. {
  96. hun.init();
  97. for (i = 1; i <= row; i++) scanf("%s", mp[i] + 1);
  98. int cnt = 0;
  99. for (i = 1; i <= row; i++)
  100. {
  101. for (j = 1; j<= col; j++)
  102. if (mp[i][j] == '*') num[i][j] = (mp[i][j] == mp[i][j - 1] ? cnt : ++cnt);
  103. }
  104. cnt = 0;
  105. for (i = 1; i <= col; i++)
  106. {
  107. for (j = 1; j <= row; j++)
  108. {
  109. if (mp[j][i] == '*')
  110. {
  111. if (mp[j][i] != mp[j - 1][i]) cnt++;
  112. hun.add_edge(cnt, num[j][i]);
  113. }
  114. }
  115. }
  116. printf("%d\n", hun.solve(cnt));
  117. }
  118. return 0;
  119. }

你可能感兴趣的:(ACM)