HDU 4888 Redraw Beautiful Drawings network flow graph, hduredraw
Question:
Given n, m, k
The following n integers a [n]
The following m integers B [n]
Use numbers [0, k] to construct a matrix of n * m
If a Unique solution exists, this matrix is output. If multiple solutions exist, Not Unique is output. If no solution exists, Impossible is output.
Idea: network stream ,,,
N rows are treated as n vertices, and m columns are treated as m vertices.
A row-column is connected to an edge with k traffic, and the Source Vertex-row is connected to an edge with a [I], and the column-sink traffic is B [I]
Blind. It's time to retire T ^ T.
# Include <stdio. h> # include <string. h> # include <iostream> # include <math. h> # include <algorithm> # include <queue> # include <vector> using namespace std; # define ll int # define N 1005 # define M 200000 # define inf 107374182 # define inf64 1152921504606846976 struct Edge {ll from, to, cap, nex;} edge [M * 2]; // note that this must be large enough. Otherwise, re will have reverse arc ll head [N], edgenum; void add (ll u, ll v, ll cap, ll rw = 0) {// if it is a directed edge, then: add (u, v, cap); if it is a undirected edge, then: ad D (u, v, cap, cap); Edge E = {u, v, cap, head [u]}; edge [edgenum] = E; head [u] = edgenum ++; Edge E2 = {v, u, rw, head [v]}; edge [edgenum] = E2; head [v] = edgenum ++;} ll sign [N]; bool BFS (ll from, ll to) {memset (sign,-1, sizeof (sign )); sign [from] = 0; queue <ll> q; q. push (from); while (! Q. empty () {ll u = q. front (); q. pop (); for (ll I = head [u]; I! =-1; I = edge [I]. nex) {ll v = edge [I]. to; if (sign [v] =-1 & edge [I]. cap) {sign [v] = sign [u] + 1, q. push (v); if (sign [to]! =-1) return true ;}}return false;} ll Stack [N], top, cur [N]; ll Dinic (ll from, ll) {ll ans = 0; while (BFS (from, to) {memcpy (cur, head, sizeof (head); ll u = from; top = 0; while (1) {if (u = to) {ll flow = inf, loc; // loc indicates the minimum cap edge in the Stack for (ll I = 0; I <top; I ++) if (flow> edge [Stack [I]. cap) {flow = edge [Stack [I]. cap; loc = I ;}for (ll I = 0; I <top; I ++) {edge [Stack [I]. cap -= Flow; edge [Stack [I] ^ 1]. cap + = flow;} ans + = flow; top = loc; u = edge [Stack [top]. from;} for (ll I = cur [u]; I! =-1; cur [u] = I = edge [I]. nex) // cur [u] indicates the subscript if (edge [I] of the edge where the u is located. cap & (sign [u] + 1 = sign [edge [I]. to]) break; if (cur [u]! =-1) {Stack [top ++] = cur [u]; u = edge [cur [u]. to;} else {if (top = 0) break; sign [u] =-1; u = edge [Stack [-- top]. from ;}}return ans;} void init () {memset (head,-1, sizeof head); edgenum = 0;} int n, m, k; int a [500], B [500], suma, sumb; int mp [505] [505], dou [505] [505]; // The dou [0] [I] I column has an incrementing vertex int hehe () {if (suma! = Sumb) return-1; init (); int from = 0, to = n + m + 10; for (int I = 1; I <= n; I ++) for (int j = 1; j <= m; j ++) add (I, n + j, k); for (int I = 1; I <= n; I ++) add (from, I, a [I]); for (int I = 1; I <= m; I ++) add (n + I,, B [I]); int flow = Dinic (from, to); if (flow! = Suma) return-1; int tt = 1; for (int I = 1; I <= n; I ++) for (int j = 1; j <= m; j ++, tt + = 2) mp [I] [j] = edge [tt]. cap; memset (dou, 0, sizeof dou); for (int I = 1; I <= n; I ++) {for (int j = 1; j <= m; j ++) for (int z = j + 1; z <= m; z ++) {bool v1 = 0, v2 = 0; if (mp [I] [j]! = K & mp [I] [z]! = 0) {if (dou [z] [j]) return 0; v1 = 1;} if (mp [I] [j]! = 0 & mp [I] [z]! = K) {if (dou [j] [z]) return 0; v2 = 1;} if (v1) dou [j] [z] = 1; if (v2) dou [z] [j] = 1 ;}} return 1 ;}void input () {suma = sumb = 0; for (int I = 1; I <= n; I ++) scanf ("% d", & a [I]), suma + = a [I]; for (int I = 1; I <= m; I ++) scanf ("% d", & B [I]), sumb + = B [I];} int main () {int u, v, I, j; while (~ Scanf ("% d", & n, & m, & k) {input (); int ans = hehe (); if (ans =-1) puts ("Impossible"); else if (ans = 0) puts ("Not Unique"); else {puts ("Unique "); for (I = 1; I <= n; I ++) for (j = 1; j <= m; j ++) printf ("% d % c ", mp [I] [j], j = m? '\ N': '') ;}} return 0 ;} /* 2 3 813 163 11 152 4 815 11 15 23 4 163 16 815 12 18 63 4 104 16 815 13 18 63 5 104 16 816 13 18 6 23 4 11 3 43 2 1 2 */