题解:
第一眼觉得是裸最小割模型,写了之后发现样例跑出个5000...
问了神犇,才知道:
1 相同地之间也得加容量为A的边,因为一开始的高低地不能决定最后策略的高低地。
2 要加双向边,因为一开始并不知道某个点应该属于S集还是T集。
复杂度:
时间复杂度:O(maxflow(n, m)),空间复杂度O(n + m)
1WA:
相同地之间也得加容量为A的边,而且得加双向边。
GET:
原来我一直不会最小割呀。
/* Telekinetic Forest Guard */ #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; const int maxg = 55, maxn = 2505, maxm = 50005, maxq = 100000, inf = 0x3f3f3f3f; int n, m, head[maxn], cur[maxn], cnt, bg, ed, A, B, depth[maxn], q[maxq]; char str[maxg]; struct _edge { int v, w, next; } g[maxm << 1]; inline void add(int u, int v, int w) { g[cnt] = (_edge){v, w, head[u]}; head[u] = cnt++; } inline void insert(int u, int v, int w) { add(u, v, w); add(v, u, 0); } inline bool bfs() { for(int i = 0; i <= ed; i++) depth[i] = -1; int h = 0, t = 0, u, i; depth[q[t++] = bg] = 1; while(h != t) for(i = head[u = q[h++]]; ~i; i = g[i].next) if(g[i].w && !~depth[g[i].v]) { depth[g[i].v] = depth[u] + 1; if(g[i].v == ed) return 1; q[t++] = g[i].v; } return 0; } inline int dfs(int x, int flow) { if(x == ed) return flow; int left = flow; for(int i = cur[x]; ~i; i = g[i].next) if(g[i].w && depth[g[i].v] == depth[x] + 1) { int tmp = dfs(g[i].v, min(g[i].w, left)); left -= tmp; g[i].w -= tmp; g[i ^ 1].w += tmp; if(g[i].w) cur[x] = i; if(!left) return flow; } if(left == flow) depth[x] = -1; return flow - left; } int main() { scanf("%d%d%d%d", &n, &m, &A, &B); bg = 0; ed = n * m + 1; for(int i = 0; i <= ed; i++) head[i] = -1; cnt = 0; for(int i = 1; i <= n; i++) { scanf("%s", str + 1); for(int j = 1; j <= m; j++) { if(str[j] == '#') insert(bg, (i - 1) * m + j, B); else insert((i - 1) * m + j, ed, B); if(i != n) insert((i - 1) * m + j, i * m + j, A), insert(i * m + j, (i - 1) * m + j, A); if(j != m) insert((i - 1) * m + j, (i - 1) * m + j + 1, A), insert((i - 1) * m + j + 1, (i - 1) * m + j, A); } } int ans = 0; while(bfs()) { for(int i = 0; i <= ed; i++) cur[i] = head[i]; ans += dfs(bg, inf); } printf("%d\n", ans); return 0; }