HDU 4888 Redraw Beautiful drawings (maximum flow)

Source: Internet
Author: User

The main topic: give you a n,m,k. n Rows, m columns. Then give you the sum of each line, with the sum of each column, so that you fill in this n*m matrix a number less than or equal to K, so as to satisfy each column, each row and. If there is no output "impossible", there is a multiple solution output "notunique", there is a unique solution output "unique", and output his solution.

From the source point to each row and the build capacity for its sum, from the sink point to the Lie Jian edge capacity for its sum. Then line to the Lie Jian edge capacity for the data cap K. Then ask if there is a full stream, if judging whether it is unique. The only time, you have to see whether there is a circulation in the middle, so that some values you will not receive the impact of how much.

Redraw Beautiful Drawings Time limit:3000/1500 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 2238 Accepted Submission (s): 507


Problem Descriptionalice and Bob are playing together. Alice was crazy about art and she had visited many museums around the world. She had a good memory and she can remember all drawings she had seen.

Today Alice designs A game using these drawings in her memory. First, she matches k+1 colors appears in the picture to k+1 different integers (from 0 to K). After that, she slices the drawing into grids and there is N rows and M columns. Each grid has a integer on it (from 0 to K) representing the color of the corresponding position in the original drawing. Alice wants to share the wonderful drawings with Bob and she tells Bob the size of the drawing, the number of different co Lors, and the sum of integers on each row and each column. Bob had to redraw the drawing with Alice ' s information. Unfortunately, Somtimes, the information Alice offers is wrong because of Alice ' s poor math. And sometimes, Bob can work out multiple different drawings using the information Alice provides. Bob gets confused and he needs your help. You had to tell Bob if Alice's information is right and if she information was right you should also tell Bob whether he C An get a unique drawing. 
Inputthe input contains mutiple testcases.

For each testcase, the first line contains three integers N (1≤n≤400), M (1≤m≤400) and K (1≤k≤40).
n integers is given in the second line representing the sum of n rows.
m integers is given in the third line representing the sum of M columns.

The input is terminated by EOF.
Outputfor each testcase, if there are no solution for Bob, output "impossible" on one line (without the quotation mark); If there is a one solution for Bob, output "Unique" in one line (without the quotation mark) and output an N * M matrix In the following N lines representing Bob ' s unique solution; If there is many ways for Bob to redraw the drawing, output ' not Unique ' in one line (without the quotation mark).
Sample Input
2 2 2  4 2 22 2 5 05 41 4 391 2 3 3

Sample Output
Not UniqueImpossibleUnique1 2 3 3

#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include < iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include < stack> #include <map> #include <set> #define EPS 1e-12///#define M 1000100#define ll __int64///#define LL Long long///#define INF 0x7ffffff#define inf 0x3f3f3f3f#define PI 3.1415926535898#define Zero (x) ((Fabs (x) <eps)? 0:x) using namespace Std;const int maxn = 1010;int cnt;int N, m;int Cur[maxn], Head[maxn];int DIS[MAXN], Gap[maxn];int AUG[MAXN    ], pre[maxn];int deep[maxn];struct node{int V, W; int next;}    F[500100];void init () {cnt = 0; Memset (Head,-1, sizeof (head));}    void Add (int u, int v, int w) {f[cnt].v = v;    F[CNT].W = W;    F[cnt].next = Head[u];    Head[u] = cnt++;    F[CNT].V = u;    F[CNT].W = 0;    F[cnt].next = Head[v]; HEAD[V] = cnt++;}    int SAP (int s, int e, int n) {int max_flow = 0, V, u = s;    int ID, Mindis;Aug[s] = INF;    Pre[s] =-1;    memset (dis, 0, sizeof (DIS));    memset (Gap, 0, sizeof (GAP));    Gap[0] = n;        for (int i = 0; I <= N; ++i) Cur[i] = head[i];///initializes the current arc to the first arc while (Dis[s] < n) {bool flag = false;            if (U = = e) {Max_flow + = Aug[e];                for (v = pre[e]; v =-1; v = pre[v])///path backtracking update residual network {id = cur[v];                F[ID].W-= Aug[e];                F[ID^1].W + = Aug[e]; AUG[V]-= aug[e]; Modify the augmented amount, which will be used later if (F[ID].W = = 0) u = V;             Do not fall back to the source point, fall back only to the arc end of the arc with a capacity of 0} for (id = cur[u]; ID! =-1; id = f[id].next)///start with current arc to find the Allow arc {            v = f[id].v;                if (f[id].w > 0 && dis[u] = = Dis[v] + 1)///Find allow ARC {flag = true;                PRE[V] = u;                CUR[U] = ID;                Aug[v] = min (Aug[u], F[ID].W);                U = V;            Break }} if (FlaG = = False) {if (--gap[dis[u] = = 0) break;///gap optimization, the hierarchy tree appears the fault is the end algorithm Mindis = n;            Cur[u] = Head[u];                for (id = head[u]; id =-1; id = f[id].next) {v = f[id].v;                    if (f[id].w > 0 && dis[v] < Mindis) {Mindis = Dis[v]; CUR[U] = ID;            Modify the current arc while modifying the label}} Dis[u] = Mindis + 1;            gap[dis[u]]++; if (U = s) u = pre[u]; Backtrack continues to look for allow arcs}} return max_flow;} int mp[410][410];int vis[maxn];bool DFS (int x, int fa) {for (int i = head[x]; I! =-1; i = f[i].next) {if (i =        = (fa^1)) continue;        if (!F[I].W) continue;        if (VIS[F[I].V]) return true;        VIS[F[I].V] = 1;        if (Dfs (F[I].V, i)) return true;    VIS[F[I].V] = 0; } return false;}    int main () {int k;    int x, y;    int S;    int T;    int en;    int sum1;    int sum2; while (~SCANF ("%d %d%d ", &n, &m, &k)) {init ();        sum1 = sum2 = 0;        S = 0;        T = n+m+1;        En = t+1;            for (int i = 1; I <= n; i++) {scanf ("%d", &x);            Sum1 + = x;            Add (S, I, X);        for (int j = 1; j <= M; j + +) Add (I, j+n, k);            } for (int i = 1; I <= m; i++) {scanf ("%d", &y);            Sum2 + = y;        Add (I+n, T, y);            } if (sum1! = sum2) {puts ("impossible");        Continue        } int ans = SAP (S, T, en);            if (ans! = sum1) {puts ("impossible");        Continue        } memset (Vis, 0, sizeof (VIS));        int flag = 0;                for (int i = 1; I <= n; i++) {if (Dfs (I,-1)) {flag = 1;            Break            }} if (flag) {puts ("not Unique");        Continue } memset (MP, 0, sizeof (mp));        Puts ("Unique");                for (int i = 1, i <= N; i++) {for (int j = head[i]; J! =-1; j = f[j].next) {                int x = F[J].V;            if (x > N && x <= n+m) mp[i][x-n] = K-F[J].W;             }} for (int i = 1; I <= n; i++) {for (int j = 1; j < M; j + +) printf ("%d", mp[i][j]);        printf ("%d\n", Mp[i][m]); }} 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.