HDU 4888 Redraw Beautiful drawings Maximum flow

Source: Internet
Author: User

Good difficult, and the ranks as X and Y, Yuanhui points to connect their respective sets, the sum of the number of columns, the equivalent of the flow from the source to each row, and then allocate traffic to each column, and finally into the sink point, so determined to infer whether the final full stream, we know there is no solution, and the solution is the flow of each row

The key is how to infer the multi-solution situation. I can't believe it, t_t.

To find a ring with a length greater than 2.

Think of a thought, that is to find the remaining flow of the ring, if found, I will be able to transfer the flow of one side of the traffic, because it is a ring, so it will reach the balance, will not destroy the maximum flow, but this transfer, the solution is a more, so just to infer whether there is a length greater than 2 of the ring is

Why the length is greater than 2, because the reverse arc when the map will cause a->b and B immediately->a, this is also a ring, but the transfer of this loop of traffic will destroy the maximum flow.

So when we use DFS to find the ring, we should be careful not to take the reverse arc.

The dinic uses the current arc optimization.

#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <  algorithm> #include <map> #include <queue> #include <vector> #include <string> #define EPS 1e-12#define INF 0x7fffffff#define maxn 1005using namespace Std;int n,m,k;int en;int st,ed;int dis[maxn],cur[maxn];int q ue[999999];struct edge{int to,c,next;};    Edge E[999999];int head[maxn];void Add (int a,int b,int c) {e[en].to=b;    E[en].c=c;    E[en].next=head[a];    head[a]=en++;    E[en].to=a;    e[en].c=0;    E[EN].NEXT=HEAD[B]; head[b]=en++;}    int BFs () {memset (dis,-1,sizeof (dis));    dis[st]=0;    int front=0,rear=0;    que[rear++]=st;        while (front<rear) {int j=que[front++];            for (int k=head[j];k!=-1;k=e[k].next) {int i=e[k].to;                if (DIS[I]==-1&AMP;&AMP;E[K].C) {Dis[i] = dis[j]+ 1;                Que[rear++]=i;            if (i==ed) return true; }}} RetuRN false;}    int dfs (int x,int mx) {if (x==ed | | mx==0) return MX;    int f,flow=0;         for (int& i=cur[x];i!=-1;i=e[i].next) {if (dis[x]+1==dis[e[i].to] && (F=dfs (E[i].to,min (MX,E[I].C))))            {e[i].c-=f;            E[i^1].c+=f;            Flow+=f;            Mx-=f;        if (!MX) break; }} return flow;}    void Init () {en=0;     st=0;     SOURCE ed=n+m+1; memset (head,-1,sizeof (Head));}    inline int ReadInt () {Char ch = getchar ();    int data = 0;    while (Ch < ' 0 ' | | ch > ' 9 ') {ch = GetChar ();        } do {data = data*10 + ch-' 0 ';    ch = getchar ();        }while (Ch >= ' 0 ' && ch <= ' 9 '); return data;}    int S1,s2;void build () {int x, y, Z;        for (int i=1;i<=n;i++) {x=readint ();        S1+=x;    Add (st,i,x);        } for (int i=1;i<=m;i++) {x=readint ();        S2+=x;    Add (i+n,ed,x); } for (int i=1;i<=n;i++) {for (int j=1;j<=m;j++) {Add (i,j+n,k);    }}}int Dinic () {int tmp=0;    int maxflow=0;        while (BFS ()) {for (int i=st;i<=ed;i++) cur[i]=head[i];    while (Tmp=dfs (St,inf)) maxflow+=tmp; } return maxflow;} int time;struct node2{int to,next;}    E2[999999];int en2,head2[maxn];void add2 (int a,int b) {e2[en2].to=b;    E2[en2].next=head2[a]; head2[a]=en2++;}        BOOL Vis[maxn];bool loop (int now,int fa) {for (int i=head2[now];~i;i=e2[i].next) {int to=e2[i].to;            if (TO!=FA) {if (Vis[to]) return true;            Vis[to]=1;            if (Loop (To,now)) return true;        vis[to]=0; }} return false;}    BOOL Cal () {en2=0;    memset (head2,-1,sizeof (head2)); for (int i=1;i<=n+m;i++) {for (int j=head[i];~j;j=e[j].next) {if (e[j].c&&e[j].to&gt            ; =1&&e[j].to<=n+m) {add2 (i,e[j].to); }}} memset (Vis,0,sizeof (VIS));    for (int i=1;i<=n;i++) {if (Loop (i,-1)) return true; } return false;}    int Ans[505];int Main () {int ca=1;        while (scanf ("%d%d%d", &n,&m,&k)!=eof) {s1=s2=0;        Init ();        Build ();        int Maxflow=dinic (); if (maxflow!=s1| |        S1!=S2) puts ("impossible");            else {if (cal ()) puts ("Not Unique");                else {puts ("Unique");                        for (int i=1;i<=n;i++) {for (int j=head[i];~j;j=e[j].next) { if (e[j].to>n&&e[j].to<=n+m) {ans[e[j].to                        -N]=K-E[J].C; }} for (int j=1;j<=m;j++) {if (j==1) PR                        intf ("%d", ans[j]);                    else printf ("%d", ans[j]);                } puts ("");           } }        }    }} 


HDU 4888 Redraw Beautiful drawings Maximum flow

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.