BZOJ3144 [Hnoi2013] cut cake minimum cut __ Network flow

Source: Internet
Author: User

"Topic link"

Learn to build a diagram.

This is a more detailed "zarxdy34"

/* telekinetic Forest Guard/#include <cstdio> #include <cstring> #include <algorithm> using Namespac

e std;

const int MAXG = 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 * + 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 (l
		EFT, 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 = n 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++) {int w = 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;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.