Hdoj 4888 Redraw Beautiful drawings "Maximum flow full stream + uniqueness judgment"

Source: Internet
Author: User

Title: Hdoj 4888 Redraw Beautiful drawings


Category: Maximum flow full flow, maximum flow uniqueness


Source: multi-university Training Contest 3


Test instructions: A matrix of each row of each column and all know, and then let you beg can fill, whether the only, the only words output solution,


Analysis: This topic can be seen as the maximum flow, but the difficulties are 2.

First of all: in the Matrix 400 * 400, fortunately, the matrix and no special requirements, so you can directly to the row and column to build a plan:

1. Source Point  - each row corresponds to a point where the traffic is limited to the row's and

2. each row corresponds to a point  - Each column corresponds to a point where the flow limit is K

3. Each column corresponds to a point  - meeting Point, the flow limit for the column and the

Time Complexity O (n^2* m) with dinci words

The 2nd is the sole judgment of the solution:

The

is given in the following: The only sufficient condition for the solution is to complete the maximum flow after the residual network is not longer than the   2  

A bit confused, but we can use the violent law to judge, that is, if J is not full stream in the same line, and K is not an empty stream, and the other row exists K is not full stream, and J is not an empty stream, think the same column is only limited, then we can give J is not empty stream minus a certain value, not the same Then k the same operation, such rows and columns and also satisfies the conditions.

Based on the above facts, we can hold a column to judge the time Complexity O (n^3)

Down Total time complexity O (n^2* (m+n)) n for 400,m = n^2, down about n^4 = 256 0000 0000 (seems a bit large)

After submission g++, C + + more than 1000, C + + unexpectedly, a bit abnormal.

This topic with EK words will time out, I began to write with Ek Super, because the EK complexity O (n*m^2) about N^5


AC Code:

#include <cstdio> #include <cstring> #include <iostream> #include <string> #include < algorithm> #include <vector> #include <queue>using namespace std; #define Del (b) memset (A,b,sizeof (a)) const int N = 1000;const int inf = 0x3f3f3f3f;int n,m,k;struct node{int from,to,cap,flow;};  Vector<int> v[n];vector<node> E;int Vis[n];    Build the hierarchy diagram int cur[n];void add_node (int from,int to,int cap) {E.push_back ((Node) {from,to,cap,0});    E.push_back (Node) {to,from,0,0});    int tmp=e.size ();    V[from].push_back (tmp-2); V[to].push_back (tmp-1);}    BOOL BFs (int s,int t) {Del (vis,-1);    Queue<int> Q;    Q.push (s);    Vis[s] = 0;        while (!q.empty ()) {int X=q.front ();        Q.pop ();            for (int i=0; i<v[x].size (); i++) {Node TMP = e[v[x][i]];                if (vis[tmp.to]<0 && tmp.cap>tmp.flow)//The second condition guarantees {vis[tmp.to]=vis[x]+1;            Q.push (tmp.to);   }     }} if (vis[t]>0) return true; return false;}    int dfs (int o,int f,int t) {if (o==t | | f==0)//optimize return F;    int a = 0,ans=0;        for (int &i=cur[o]; i<v[o].size (); i++)//Note front ' & ', very important optimization {Node &tmp = e[v[o][i]];            if (vis[tmp.to]== (vis[o]+1) && (a = DFS (Tmp.to,min (f,tmp.cap-tmp.flow), T)) >0) {tmp.flow+=a; E[v[o][i]^1].flow-=a;            Ans+=a of the mode of storage and mapping;            F-=a;        if (f==0)//attention optimization break;  }} return ans;    Optimized}int dinci (int s,int t) {int ans=0;        while (BFS (s,t)) {Del (cur,0);        int Tm=dfs (S,INF,T);    Ans+=tm; } return ans;        int mp[450][450];int Dou[450][450];bool Solve () {for (int i=1;i<=n;i++) {int tmp=0;    for (int j=0;j<v[i].size (); j + +)//Note standard if (e[v[i][j]].to>n) Mp[i][++tmp]=e[v[i][j]].flow;    } Del (dou,0); for (int i = 1; I <= n; i++)//violence method according to full flow empty flowJudgment {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;}        int main () {while (~scanf ("%d%d%d", &n,&m,&k)) {int x;        int s=0,t=m+n+1,sum_a=0,sum_b=0;            for (int i=1;i<=n;i++) {scanf ("%d", &x);            Sum_a + = x;            Add_node (S,I,X);        for (int j=1;j<=m;j++) Add_node (i,n+j,k);            } for (int i=1;i<=m;i++) {scanf ("%d", &x);            Sum_b+=x; Add_node (i+n, t,x);        } int Ans=dinci (S,T);        if (Ans!=min (Sum_a,sum_b)) printf ("impossible\n");            else {memset (mp,0,sizeof (MP));            if (Solve () ==0) printf ("Not unique\n");                else {printf ("unique\n"); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) printf ("%d%c", mp[i][j],j==m? '            \ n ': ');        }} for (int i=0; i<=t; i++) v[i].clear ();    E.clear (); } return 0;}


Hdoj 4888 Redraw Beautiful drawings "Maximum flow full stream + uniqueness judgment"

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.