标签:nbsp lib 整数 printf strong ;; img 规模 name
[codevs1022]覆盖
试题描述
有一个N×M的单位方格中,其中有些方格是水塘,其他方格是陆地。如果要用1×2的矩阵区覆盖(覆盖过程不容许有任何部分重叠)这个陆地,那么最多可以覆盖多少陆地面积。
输入
输入文件的第一行是两个整数N,M (1<=N,M<=100),第二行为一个整数K( K<=50),接下来的K行,每行两个整数X,Y表示K个水塘的行列位置。(1<=X<=N,1<=Y<=M)。
输出
输出所覆盖的最大面积块(1×2面积算一块)。
输入示例
4 4 6 1 1 1 4 2 2 4 1 4 2 4 4
输出示例
4
数据规模及约定
见“输入”
题解
黑白染色后,挖去那几个被排除的点跑二分图匹配。
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <algorithm> using namespace std; int read() { int x = 0, f = 1; char c = getchar(); while(!isdigit(c)){ if(c == ‘-‘) f = -1; c = getchar(); } while(isdigit(c)){ x = x * 10 + c - ‘0‘; c = getchar(); } return x * f; } #define maxn 10010 #define maxm 160010 #define oo 2147483647 struct Edge { int from, to, flow; Edge() {} Edge(int _1, int _2, int _3): from(_1), to(_2), flow(_3) {} } ; struct Dinic { int n, m, s, t, head[maxn], next[maxm]; Edge es[maxm]; int hd, tl, Q[maxn], vis[maxn]; int cur[maxn]; void init(int nn) { n = nn; m = 0; memset(head, -1, sizeof(head)); return ; } void AddEdge(int a, int b, int c) { es[m] = Edge(a, b, c); next[m] = head[a]; head[a] = m++; return ; } bool BFS() { memset(vis, 0, sizeof(vis)); vis[s] = 1; hd = tl = 0; Q[++tl] = s; while(hd < tl) { int u = Q[++hd]; for(int i = head[u]; i != -1; i = next[i]) { Edge& e = es[i]; if(!vis[e.to] && e.flow) { vis[e.to] = vis[u] + 1; Q[++tl] = e.to; } } } return vis[t] > 1; } int DFS(int u, int a) { if(u == t || !a) return a; int flow = 0, f; for(int& i = cur[u]; i != -1; i = next[i]) { Edge& e = es[i]; if(vis[e.to] == vis[u] + 1 && (f = DFS(e.to, min(a, e.flow)))) { flow += f; a -= f; e.flow -= f; es[i^1].flow += f; if(!a) return flow; } } return flow; } int MaxFlow(int ss, int tt) { s = ss; t = tt; int flow = 0; while(BFS()) { for(int i = 1; i <= n; i++) cur[i] = head[i]; flow += DFS(s, oo); } return flow; } } sol; #define maxs 110 bool Map[maxs][maxs]; int main() { int n = read(), m = read(), k = read(); sol.init(n * m + 2); int s = n * m + 1, t = s + 1; for(int i = 1; i <= k; i++) { int a = read() - 1, b = read() - 1; Map[a][b] = 1; } for(int i = 0; i < n; i++) for(int j = 0; j < m - 1; j++) if(!Map[i][j] && !Map[i][j+1]) { int id = i * m + j + 1, rid = i * m + j + 2; sol.AddEdge(id, rid, 1); sol.AddEdge(rid, id, 1); } for(int i = 0; i < n - 1; i++) for(int j = 0; j < m; j++) if(!Map[i][j] && !Map[i+1][j]) { int id = i * m + j + 1, did = (i+1) * m + j + 1; sol.AddEdge(id, did, 1); sol.AddEdge(did, id, 1); } for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(!Map[i][j]) { int id = i * m + j + 1; if((i & 1) ^ (j & 1)) sol.AddEdge(s, id, 1), sol.AddEdge(id, s, 0); else sol.AddEdge(id, t, 1), sol.AddEdge(t, id, 0); } printf("%d\n", sol.MaxFlow(s, t)); return 0; }
标签:nbsp lib 整数 printf strong ;; img 规模 name
原文地址:http://www.cnblogs.com/xiao-ju-ruo-xjr/p/6034873.html