Template (network stream judgment: whether there is a matrix of always rows and columns)

Source: Internet
Author: User



The template is as follows: // HDU 4888: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4888

# Include <cstdio> # include <iostream> # include <cstring> # include <algorithm> # include <cmath> # include <queue> using namespace STD; # define ll _ int64 # define EPS 1e-8const ll mod = (1e9 + 7); const int maxn = 510; const int maxm = 50100; int n, m, K; int R [maxn], C [maxn]; int ma [maxn] [maxn]; const int maxnode = 1000 + 5; const int maxedge = 2*161000 + 5; const int oo = 1000000000; int node, SRC, DEST, nedge; int head [Max Node], point [maxedge], next1 [maxedge], flow [maxedge], Capa [maxedge]; // point [x] = y indicates that the X side is connected to y, head, next is the adjacent table, flow [x] represents the dynamic value of X edge, Capa [x] represents the initial value of X edge int Dist [maxnode], Q [maxnode], work [maxnode]; // Dist [I] indicates the I point level void Init (INT _ node, int _ SRC, int _ DEST) {// initialization, node indicates the number of vertices, Src indicates the start point, and DEST indicates the end node = _ node; src = _ SRC; DEST = _ DEST; For (INT I = 0; I <node; I ++) head [I] =-1; nedge = 0;} void addedge (INT U, int V, int C1, int C2) {// Add one edge point [nedge] = V, Capa [nedge] = C1, flow [nedge] = 0, next1 [nedge] = head [u], head [u] = (nedge ++); point [nedge] = u, capa [nedge] = c2, flow [nedge] = 0, next1 [nedge] = head [v], head [v] = (nedge ++);} bool dinic_bfs () {memset (Dist, 255, sizeof (DIST); Dist [SRC] = 0; int sizeq = 0; Q [sizeq ++] = SRC; for (INT Cl = 0; CL <sizeq; CL ++) for (int K = Q [Cl], I = head [K]; I> = 0; I = next1 [I]) if (flow [I] <Capa [I] & Dist [point [I] <0) {Dist [point [I] = DIST [k] + 1; Q [sizeq ++] = point [I];} return Dist [DEST]> = 0 ;} int dinic_dfs (int x, int exp) {If (x = DEST) return exp; For (Int & I = work [X]; I> = 0; I = next1 [I]) {int v = point [I], TMP; if (flow [I] <Capa [I] & Dist [v] = DIST [x] + 1 & (TMP = dinic_dfs (v, min (exp, capa [I]-flow [I])> 0) {flow [I] + = TMP; Flow [I ^ 1]-= TMP; return TMP;} return 0;} int dinic_flow () {int result = 0; while (dinic_bfs ()) {for (INT I = 0; I <node; I ++) work [I] = head [I]; while (1) {int Delta = dinic_dfs (SRC, OO); If (delta = 0) break; Result + = delta;} return result;} // run init () again before creating the graph (); // when edge is added, run addedge (a, B, c, 0), which indicates that the traffic from vertex A to vertex B is set to C (note that the vertex serial number starts from 0) // run dinic_flow () to solve the maximum stream. The returned value is bool judge (INT sumrow) {int flow = 1; for (INT I = 1; I <= N; I ++) for (Int J = n + 1; j <= N + m; j ++) addedge (I, j, k, 0); flow = dinic_flow (); If (flow! = Sumrow) return false; return true;} int main () {// K is the maximum number that can be filled in the source image while (scanf ("% d ", & N, & M, & K )! = EOF) {Init (n + m + 2, 0, N + m + 1); int flag = 0; int sumrow = 0, colrow = 0; For (INT I = 1; I <= N; I ++) {scanf ("% d", & R [I]); addedge (0, I, R [I], 0 ); sumrow + = R [I]; If (R [I] <0 | r [I]> M * k) Flag = 1 ;}for (Int J = 1; j <= m; j ++) {scanf ("% d", & C [J]); addedge (J + N, N + m + 1, c [J], 0); colrow + = C [J]; If (C [J] <0 | C [J]> N * k) Flag = 1 ;} if (sumrow! = Colrow) {printf ("impossible \ n"); continue;} If (! Judge (sumrow) Flag = 1; if (flag = 1) {printf ("impossible \ n"); continue;} memset (MA,-1, sizeof (MA); int I, j; for (I = 1; I <= N; I ++) if (R [I] = 0) for (j = 1; j <= m; j ++) Ma [I] [J] = 0; For (j = 1; j <= m; j ++) if (C [J] = 0) for (I = 1; I <= N; I ++) Ma [I] [J] = 0; int TT = 2; int sum, num, temp; while (TT --) {for (I = 1; I <= N; I ++) {If (R [I] = 0) {for (j = 1; j <= m; j ++) if (MA [I] [J] =-1) Ma [I] [J] = 0; continue;} sum = 0; num = 0; For (j = 1; j <= m; j ++) {I F (MA [I] [J] =-1) {num ++; temp = J; sum + = min (K, C [J]) ;}} if (num = 1) {ma [I] [temp] = R [I]; R [I]-= ma [I] [temp]; c [temp]-= ma [I] [temp]; continue;} else if (sum = R [I]) {for (j = 1; j <= m; j ++) {If (MA [I] [J] =-1) {ma [I] [J] = min (K, C [J]); R [I]-= ma [I] [J]; C [J]-= ma [I] [J] ;}}} for (j = 1; j <= m; j ++) {If (C [J] = 0) {for (I = 1; I <= N; I ++) if (MA [I] [J] =-1) Ma [I] [J] = 0; continue;} sum = 0; num = 0; for (I = 1; I <= N; I ++) {If (MA [I] [J] = =-1) {num ++; temp = I; sum + = min (K, R [I]) ;}} if (num = 1) {ma [temp] [J] = C [J]; R [temp]-= ma [temp] [J]; c [J]-= ma [temp] [J]; continue;} else if (sum = C [J]) {for (I = 1; I <= N; I ++) {If (MA [I] [J] =-1) {ma [I] [J] = min (K, R [I]); R [I]-= ma [I] [J]; C [J]-= ma [I] [J] ;}}} flag = 0; for (I = 1; I <= N; I ++) if (R [I]! = 0) {flag = 1; break;} For (j = 1; j <= m; j ++) if (C [J]! = 0) {flag = 1; break;} If (flag = 1) printf ("not unique \ n"); else {printf ("unique \ n "); for (I = 1; I <= N; I ++) {for (j = 1; j <m; j ++) printf ("% d ", ma [I] [J]); printf ("% d \ n", ma [I] [m]) ;}} return 0 ;}



Template (network stream judgment: whether there is a matrix of always rows and columns)

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.