BZOJ3144 [Hnoi2013]切糕最小割 __網路流

來源:互聯網
上載者:User

【題目連結】

學習一發建圖。

這篇題解比較詳細【zarxdy34的題解】

/* Telekinetic Forest Guard */#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxg = 45, maxn = 70005, maxm = 150005, maxq = 100000, inf = 0x3f3f3f3f;int n, m, h, d, head[maxn], cur[maxn], cnt, bg, ed, depth[maxn], q[maxq], id[maxg][maxg][maxg];int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};struct _edge {int v, w, next;} g[maxm << 1];inline int iread() {int f = 1, x = 0; char ch = getchar();for(; ch < '0' || ch > '9'; ch = getchar()) f = ch == '-' ? -1 : 1;for(; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';return f * x;}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(left, g[i].w));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() {n = iread(); m = iread(); h = iread(); d = iread();for(int i = 0; i < maxn; i++) head[i] = -1; cnt = 0;int tot = 0; bg = ++tot;for(int i = 1; i <= h + 1; i++) for(int j = 1; j <= n; j++) for(int k = 1; k <= m; k++)id[i][j][k] = ++tot;ed = ++tot;for(int i = 1; i <= h; i++) for(int j = 1; j <= n; j++) for(int k = 1; k <= m; k++) {intw = iread();insert(id[i][j][k], id[i + 1][j][k], w);if(i + d <= h + 1) for(int r = 0; r < 4; r++) {int x = j + dx[r], y = k + dy[r];if(x < 1 || x > n || y < 1 || y > m) continue;insert(id[i + d][x][y], id[i][j][k], inf);}}for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) insert(bg, id[1][i][j], inf), insert(id[h + 1][i][j], ed, inf);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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.