ZOJ1994 has Yuanhui feasible flow in the upper and lower bounds

Source: Internet
Author: User
Tags cmath

Http://fastvj.rainng.com/contest/236779#problem/G

Description:

N Rows M column

Give you the line and with columns

Then there is the Q limit, which indicates the range of the size of the specific cell element, and finally asks you the possible matrix values

Solution:
There is a Yuanhui upper and lower boundary maximum flow problem, the initial source point of the line and traffic is the row corresponding to the row and then the column with the initial sink point, the capacity of the column columns, the detailed restrictions, corresponding to the rows and columns, because I finally want to output a matrix, so n * m each edge to link finally, manually connected to a T Becomes no Yuanhui has the upper and lower bounds feasible flow problem, then splits, the corresponding side flow, puts in the array, the output is good

Code:

Dinic, data manipulation function, plus edge operation in front of the comparison base

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #define INF (1 <<) using namespace std;const int maxn = 25;const int MAXM = 210;con St int MN = 505;const int mm = 440020;struct node{int to,val,pre,lid;} E[mm];int id[mn],cnt=0;int cur[mn];int flor[mn];int upflow[mn];int lowf[maxm][maxn];int upf[maxm][maxn];int OUT[MAXM]    [Maxn];void init () {memset (id,-1,sizeof (id));    memset (upflow,0,sizeof (Upflow)); CNT = 0;}    void Add (int from,int to,int val,int lid) {e[cnt].to = to;    E[cnt].val = val;    E[cnt].lid = lid;    E[cnt].pre = Id[from];    Id[from] = cnt++;    Swap (from,to);    E[cnt].to = to;    E[cnt].val = 0;    E[cnt].lid = lid;    E[cnt].pre = Id[from]; Id[from] = cnt++;}    void Addflow (int from,int to,int low,int up,int lid) {Upflow[from]-= low;    Upflow[to] + = low; Add (from,to,up-low,lid);}    BOOL BFs (int s,int t) {memset (flor,0,sizeof (Flor));    Flor[s] = 1; QuEue<int> Q;    while (Q.size ()) Q.pop ();    Q.push (s);        while (Q.size ()) {Int. now = Q.front ();        Q.pop ();            for (int i = Id[now];~i;i = e[i].pre) {int to = e[i].to;            int val = e[i].val;                if (Val > 0 && flor[to] = = 0) {Flor[to] = Flor[now] + 1;                Q.push (to);            if (to = = T) return true; }}} return false;}    int dfs (int s,int t,int value) {if (s = = T | | value = = 0) return value;    int ret = Value,a;        for (int &i = Cur[s];~i;i = e[i].pre) {int val = e[i].val;        int to = e[i].to;            if (flor[to] = = Flor[s] + 1 && (a = DFS (To,t,min (ret,val)))) {e[i].val = A;            E[i^1].val + = A;            RET-= A;        if (ret = = 0) break;    }} if (ret = = value) Flor[s] = 0; return Value-ret;}    int dinic (int s,int t) {int ret = 0;        while (BFS (s,t)) {memcpy (cur,id,sizeof (id));    RET + = DFS (s,t,inf); } return ret;}    void Addlimit (int i,int J,char Op,int Lim) {if (op = = ' = ') {Upf[i][j] = lowf[i][j] = Lim;    } else if (op = = ' > ') {lowf[i][j] = max (lowf[i][j],lim+1);    } else {Upf[i][j] = min (upf[i][j],lim-1); }}

And then depending on the input a little bit of edge, which is the row and column and the corresponding edge

scanf ("%d%d", &n,&m);        s = 0;        t = n + m + 1;        SS = t + 1;        TT = SS + 1;        int lsum;        for (int i = 1;i <= n;++i)        {            scanf ("%d", &lsum);            Addflow (s,i,lsum,lsum,0);        }        for (int i = 1;i <= m;++i)        {            scanf ("%d", &lsum);            Addflow (n + i,t,lsum,lsum,0);        }        int limitnum;

Then, based on the constraints given in the topic, populate the upper and lower bounds array

scanf ("%d", &limitnum);                for (int i = 1;i <= n;++i) {for (int j = 1;j <= m;++j) {lowf[i][j] = 0;            UPF[I][J] = inf;        }} int Row,col,lim;        Char op;            for (int i = 1;i <= limitnum;++i) {scanf ("%d%d%c%d", &row,&col,&op,&lim);                    if (row = = 0 && col = = 0) {for (int i = 1;i <= n;++i) {                    for (int j = 1;j <= m;++j) {addlimit (I,j,op,lim);                }}} else if (row = = 0) {for (int i = 1;i <= n;++i)                {Addlimit (I,col,op,lim);                    }} else if (col = = 0) {for (int i = 1;i <= m;++i) {                Addlimit (Row,i,op,lim);         }   } else {addlimit (Row,col,op,lim); }        }

The upper and lower bounds are added according to the upper and lower bounds array

int tot = 0;        for (int i = 1;i <= n;++i)        {for            (int j = 1;j <= m;++j)            {                addflow (i,n+j,lowf[i][j],upf[i][j],++tot);            }        }

When the addition edge is completed, it is transformed into a feasible flow with no Yuanhui and upper and lower bounds ———— cyclic flow

Add (t,s,inf,0);        int sum = 0;        for (int i = s;i <= t;++i)        {            if (Upflow[i] < 0)            {                Add (i,tt,-upflow[i],0);            }            else            {                sum + = Upflow[i];                Add (ss,i,upflow[i],0);            }        }

Run Diinc, if there is a viable flow.

Just output BA ~ ~

for (int now = N+1;now <= n + m;++now)            {for                (int i = Id[now];~i;i = E[i].pre)                {                    int to = e[i].to;                    int lid = E[i].lid;                    if (lid = = 0 | | I% 2 = 0) continue;                    Out[to][now-n] = Lowf[to][now-n] + e[i].val;                }            }            for (int i = 1;i <= n;++i)            {                for (int j = 1;j <= m;++j)                {                    if (j = = 1)                        printf ("%d", out[i][j]); C15/>else                        printf ("%d", out[i][j]);                }                printf ("\ n");            }

Full code

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <queue> #define INF (1 <<) using namespace std;const int maxn = 25;const int MAXM = 210;con St int MN = 505;const int mm = 440020;struct node{int to,val,pre,lid;} E[mm];int id[mn],cnt=0;int cur[mn];int flor[mn];int upflow[mn];int lowf[maxm][maxn];int upf[maxm][maxn];int OUT[MAXM]    [Maxn];void init () {memset (id,-1,sizeof (id));    memset (upflow,0,sizeof (Upflow)); CNT = 0;}    void Add (int from,int to,int val,int lid) {e[cnt].to = to;    E[cnt].val = val;    E[cnt].lid = lid;    E[cnt].pre = Id[from];    Id[from] = cnt++;    Swap (from,to);    E[cnt].to = to;    E[cnt].val = 0;    E[cnt].lid = lid;    E[cnt].pre = Id[from]; Id[from] = cnt++;}    void Addflow (int from,int to,int low,int up,int lid) {Upflow[from]-= low;    Upflow[to] + = low; Add (from,to,up-low,lid);}    BOOL BFs (int s,int t) {memset (flor,0,sizeof (Flor));    Flor[s] = 1; QuEue<int> Q;    while (Q.size ()) Q.pop ();    Q.push (s);        while (Q.size ()) {Int. now = Q.front ();        Q.pop ();            for (int i = Id[now];~i;i = e[i].pre) {int to = e[i].to;            int val = e[i].val;                if (Val > 0 && flor[to] = = 0) {Flor[to] = Flor[now] + 1;                Q.push (to);            if (to = = T) return true; }}} return false;}    int dfs (int s,int t,int value) {if (s = = T | | value = = 0) return value;    int ret = Value,a;        for (int &i = Cur[s];~i;i = e[i].pre) {int val = e[i].val;        int to = e[i].to;            if (flor[to] = = Flor[s] + 1 && (a = DFS (To,t,min (ret,val)))) {e[i].val = A;            E[i^1].val + = A;            RET-= A;        if (ret = = 0) break;    }} if (ret = = value) Flor[s] = 0; return Value-ret;}    int dinic (int s,int t) {int ret = 0;        while (BFS (s,t)) {memcpy (cur,id,sizeof (id));    RET + = DFS (s,t,inf); } return ret;}    void Addlimit (int i,int J,char Op,int Lim) {if (op = = ' = ') {Upf[i][j] = lowf[i][j] = Lim;    } else if (op = = ' > ') {lowf[i][j] = max (lowf[i][j],lim+1);    } else {Upf[i][j] = min (upf[i][j],lim-1);    }}int Main () {int T;    scanf ("%d", &t);    int n,m,s,t,ss,tt;        while (t--) {init ();        scanf ("%d%d", &n,&m);        s = 0;        t = n + m + 1;        SS = T + 1;        TT = SS + 1;        int lsum;            for (int i = 1;i <= n;++i) {scanf ("%d", &lsum);        Addflow (s,i,lsum,lsum,0);            } for (int i = 1;i <= m;++i) {scanf ("%d", &lsum);        Addflow (n + i,t,lsum,lsum,0);        } int limitnum;        scanf ("%d", &limitnum);              for (int i = 1;i <= n;++i) {for (int j = 1;j <= m;++j) {lowf[i][j] = 0;  UPF[I][J] = inf;        }} int Row,col,lim;        Char op;            for (int i = 1;i <= limitnum;++i) {scanf ("%d%d%c%d", &row,&col,&op,&lim);                    if (row = = 0 && col = = 0) {for (int i = 1;i <= n;++i) {                    for (int j = 1;j <= m;++j) {addlimit (I,j,op,lim);                }}} else if (row = = 0) {for (int i = 1;i <= n;++i)                {Addlimit (I,col,op,lim);                    }} else if (col = = 0) {for (int i = 1;i <= m;++i) {                Addlimit (Row,i,op,lim);            }} else {addlimit (Row,col,op,lim);        }} int tot = 0;        for (int i = 1;i <= n;++i) {    for (int j = 1;j <= m;++j) {addflow (I,n+j,lowf[i][j],upf[i][j],++tot);        }} Add (t,s,inf,0);        int sum = 0;            for (int i = S;i <= t;++i) {if (Upflow[i] < 0) {Add (i,tt,-upflow[i],0);                } else {sum + = Upflow[i];            Add (ss,i,upflow[i],0);                }} if (Dinic (ss,tt) = = sum) {for (int. now = N+1;now <= n + m;++now) {                    for (int i = Id[now];~i;i = e[i].pre) {int to = e[i].to;                    int lid = E[i].lid;                    if (lid = = 0 | | I% 2 = 0) continue;                Out[to][now-n] = Lowf[to][now-n] + e[i].val;                }} for (int i = 1;i <= n;++i) {for (int j = 1;j <= m;++j) {if (j = = 1) printf ("%d", out[i][j]);                else printf ("%d", out[i][j]);            } printf ("\ n");        }} else {printf ("impossible\n");    } if (T) printf ("\ n"); } return 0;}

ZOJ1994 has Yuanhui feasible flow in the upper and lower bounds

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.