Hduoj--4888--redraw Beautiful Drawings "ISAP" network flow + judgment ring

Source: Internet
Author: User

Links:http://acm.hdu.edu.cn/showproblem.php?pid=4888

Test instructions: a matrix, limited to each row and, columns, each lattice number does not exceed K, ask whether the matrix exists, if there is a single solution or multiple solutions.


train of thought: before the topic of more than the school, at that time will not network flow, now a dropped, the matrix of the model, to determine whether the network flow is feasible as long as the maximum flow is determined to be equal to the head row and or the total column and can, the sentence is to see other people's problem-solving report, The method is to use DFS to find the residual network in the presence of the capacity of the arc into a ring, if any, the description can be changed through this ring in the capacity of the network of the internal way of augmentation, and Yuanhui traffic will not change, it shows that there are many solutions. If there is no ring, it is a single solution.

Map: The source points arc to each row node, with the capacity of the row and, each column node to the sink point, the capacity for each column and, each row node and each column node, the capacity is K.

Details: I used the ISAP of the topic before the template, but WA, I debug after debugging found that the error will not be in the map and sentence, and then the other people's ISAP code control, found a different place, change the AC, do not understand why, not the same place code has comments. My understanding of nn points, the source point src level is 0, then the largest level should be nn-1, but minm initialized to Nn-1 is WA, initialized to nn AC. Theoretically it should be initialized greater than or equal to Nn-1 is OK, later or on the initialization of the NN bar to avoid inexplicable WA.


#include <cstring> #include <string> #include <fstream> #include <iostream> #include < iomanip> #include <cstdio> #include <cctype> #include <algorithm> #include <queue> #include <map> #include <set> #include <vector> #include <stack> #include <ctime> #include < cstdlib> #include <functional> #include <cmath>using namespace std; #define PI ACOs ( -1.0) #define MAXN 50100#define EPS 1e-7#define INF 0x7fffffff#define llinf 0x7fffffffffffffff#define seed 131#define MOD 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct node{int u,v,w,n ext;} Edge[500000];int head[820],dist[820],cur[820],fa[820],num[820],vis[820];int n,m,k,cnt,nn,src,sink;void Add_edge (    int A,int b,int c) {edge[cnt].u = A;    EDGE[CNT].V = b;    EDGE[CNT].W = C;    Edge[cnt].next = Head[a]; Head[a] = cnt++;}    void BFs () {int x,i,j;    Queue<int> Q; memset (dist,-1,sizeof (Dist));    memset (num,0,sizeof (num));    Q.push (sink);    Dist[sink] = 0;    Num[0] = 1;        while (!q.empty ()) {x = Q.front ();        Q.pop ();                for (I=head[x];i!=-1;i=edge[i].next) {if (dist[edge[i].v]<0) {DIST[EDGE[I].V] = dist[x] + 1;                num[dist[edge[i].v]]++;            Q.push (EDGE[I].V);    }}}}int augment () {int x=sink,a=inf;        while (X!=SRC) {a = min (A,EDGE[FA[X]].W);    x = edge[fa[x]].u;    } X=sink;        while (X!=SRC) {edge[fa[x]].w-= A;        EDGE[FA[X]^1].W + = A;    x = edge[fa[x]].u; } return A;    int Isap () {int i,x,ok,minm,flow=0;    BFS ();    for (i=0;i<=nn+5;i++) cur[i] = Head[i];    X=SRC;            while (DIST[SRC]&LT;NN) {if (X==sink) {flow + = augment ();        x = src;        } ok=0;                for (I=cur[x];i!=-1;i=edge[i].next) {if (EDGE[I].W && dist[x]==dist[edge[i].v]+1) {ok=1; FA[EDGE[I].V] = i;                CUR[X] = i;                x = EDGE[I].V;            Break                }} if (!ok) {minm = NN;//MINM = nn-1 on WA for (I=head[x];i!=-1;i=edge[i].next)            if (EDGE[I].W && dist[edge[i].v]<minm) MINM=DIST[EDGE[I].V];            if (--num[dist[x]]==0) break;            num[dist[x]=minm+1]++;            CUR[X]=HEAD[X];        if (X!=SRC) x=edge[fa[x]].u; }} return flow;}    BOOL Dfs (int u,int pre) {int i,j;    if (Vis[u]) return true;    Vis[u] = 1;            for (I=head[u];i!=-1;i=edge[i].next) {if (Edge[i].w>0&&edge[i].v!=pre&&dfs (edge[i].v,u))    return true;    } Vis[u] = 0; return false;}    int Row[410],col[410];int Ans[420][420];int Main () {int i,j;    int SUMR,SUMC;        while (scanf ("%d%d%d", &n,&m,&k)!=eof) {memset (head,-1,sizeof (head));        SUMR = SUMC = 0;        CNT = 0;        src = 0;        Sink = n + M + 1;    nn = sink + 1;    for (i=1;i<=n;i++) {scanf ("%d", &row[i]);            SUMR + = Row[i];            Add_edge (Src,i,row[i]);            Add_edge (i,src,0);                for (j=1;j<=m;j++) {Add_edge (i,j+n,k);            Add_edge (j+n,i,0);            }} for (i=1,j=n+1;i<=m;j++,i++) {scanf ("%d", &col[i]);            SUMC + = Col[i];            Add_edge (J,sink,col[i]);        Add_edge (sink,j,0);            } if (SUMR!=SUMC) {puts ("impossible");        Continue        } int flag = 0;        int flow = ISAP ();            if (FLOW!=SUMR) {puts ("impossible");        Continue        } memset (Vis,0,sizeof (VIS));                for (i=1;i<=n;i++) {if (Dfs (i,-1)) {flag = 1;            Break            }} if (flag) {puts ("not Unique");        Continue        } puts ("Unique");        memset (ans,0,sizeof (ans));         for (i=1;i<=n;i++) {   for (j=head[i];j!=-1;j=edge[j].next) {int u = EDGE[J].V;                if (u>n&&u<=n+m) {ans[i][u-n] = K-EDGE[J].W; }}} for (i=1;i<=n;i++) {for (j=1;j<=m;j++) {if (j>1) printf (""                );            printf ("%d", ans[i][j]);        } printf ("\ n"); }} return 0;}



Hduoj--4888--redraw Beautiful Drawings "ISAP" network flow + judgment ring

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.