Poj2396 budget upstream and downstream network feasible streams

Source: Internet
Author: User
Creating a graph is troublesome... Really troublesome... It's too troublesome. Note> + 1, <-1, it should be possible to obtain the upper and lower limits of the upstream and downstream networks. Here I sent a wa to talk about the structure of the original network, the secondary network can be completed on this basis, which is a little more troublesome. The Source Vertex and the row are connected. The upper and lower bounds of the edge are the sum of all numbers in the row. The upper and lower bounds of the edge are the sum of all numbers in the column. Then, each row and each column must be connected. The initial upper limit is INF, and the initial lower limit is 0. Then, adjust the upper and lower limits according to the constraints given in the question. Budget
Time limit:3000 Ms   Memory limit:65536 K
Total submissions:4384   Accepted:1681   Special Judge

Description

We are supposed to make a budget proposal for this multi-site competition. the budget proposal is a matrix where the rows represent different kinds of expenses and the columns represent different sites. we had a meeting about this,
Some time ago where we discussed the sums over different kinds of expenses and sums over different sites. there was also some talk about special constraints: Someone mentioned that computer center wowould need at least 2000 K rials for food and someone from Sharif
Authorities argued they wouldn't use more than 30000 K rials for T-shirts. Anyway, we are sure there was more; we will go and try to find some notes from that meeting.

And, by the way, no one really reads budget proposals anyway, so we'll just have to make sure that it sums up properly and meets all constraints.

Input

The first line of the input contains an integer N, giving the number of test cases. the next line is empty, then, test cases follow: the first line of each test case contains two integers, M and N, giving the number of rows and
Columns (M <= 200, n <= 20 ). the second line contains M integers, giving the row sums of the matrix. the third line contains N integers, giving the column sums of the matrix. the fourth line contains an integer c (C <1000) giving the number of constraints.
The next C lines contain the constraints. There is an empty line after each test case.

Each constraint consists of two integers R and Q, specifying some entry (or entries) in the matrix (the upper left corner is 1 1 and 0 is interpreted as "all", I. e. 4 0 means all entries on the fourth row and 0 0 means the entire matrix), one element from
Set {<, =, >}and one integer V, with the obvious interpretation. for instance, the constraint 1 2> 5 means that the cell in the 1st row and 2nd column must have an entry strictly greater than 5, and the constraint 4 0 = 3 means that all elements in the fourth
Row shoshould be equal to 3.

Output

For each case output a matrix of non-negative integers meeting the above constraints or the string "impossible" if no legal solution exists. Put
One empty lineBetween matrices.

Sample Input

22 3 8 10 5 6 7 4 0 2 > 2 2 1 = 3 2 3 > 2 2 3 < 5 2 2 4 5 6 7 1 1 1 > 10

Sample output

2 3 3 3 3 4 IMPOSSIBLE 
#include<iostream>#include<cstdio>#include<algorithm>#include<string>#include<cstring>using namespace std;#define MAXN 500 #define INF 0xFFFFFF#define MAXM 9999999struct edge{int to,c,next;}e[MAXM];struct node{int c,l;}matrix[250][50];int head[MAXN],en,l[MAXN];int dis[MAXN],gap[MAXN],st,ed;int n,m,vn;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 isap(int u,int flow) {    if(u==ed)return flow;    int j,mindis=vn-1,t=flow,d;    for(j=head[u];j!=-1;j=e[j].next) {        int v=e[j].to,val=e[j].c;        if(val>0) {            if(dis[v]+1==dis[u]) {                if(t<e[j].c)                     d=t;else                     d=e[j].c;                d=isap(v,d);                e[j].c-=d,e[j^1].c+=d;                t-=d;                if(dis[st]>=vn)                    return flow-t;                if(t==0)                     break;            }            if(dis[v]<mindis)                mindis=dis[v];        }    }    if(t==flow) {        --gap[dis[u]];        if (gap[dis[u]]==0)             dis[st]=vn;        dis[u]=mindis+1;        ++gap[dis[u]];    }    return flow-t;}  void solve(){int a,b,c,q;char str[5];en=0;scanf("%d%d",&m,&n);memset(matrix,0,sizeof(matrix));memset(l,0,sizeof(l));memset(head,-1,sizeof(head));for(int i=1;i<=m;i++)for(int j=1;j<=n;j++)matrix[i][j].c=INF;for(int i=1;i<=m;i++){scanf("%d",&matrix[i][0].c);matrix[i][0].l=matrix[i][0].c;}for(int i=1;i<=n;i++){scanf("%d",&matrix[0][i].c);matrix[0][i].l=matrix[0][i].c;}scanf("%d",&q);bool flag=false;while(q--){scanf("%d%d%s%d",&a,&b,&str,&c);if(flag)continue;if(str[0]=='<'){if(a && b){matrix[a][b].c=min(c-1,matrix[a][b].c);if(matrix[a][b].c<matrix[a][b].l) flag=true;}if(!a && !b){for(int i=1;i<=m;i++)for(int j=1;j<=n;j++){matrix[i][j].c=min(c-1,matrix[i][j].c);if(matrix[i][j].c<matrix[i][j].l) flag=true;}}if(!a && b){for(int i=1;i<=m;i++){matrix[i][b].c=min(c-1,matrix[i][b].c);if(matrix[i][b].c<matrix[i][b].l) flag=true;}}if(a && !b){for(int i=1;i<=n;i++){matrix[a][i].c=min(c-1,matrix[a][i].c);if(matrix[a][i].c<matrix[a][i].l) flag=true;}}}if(str[0]=='>'){if(a && b){matrix[a][b].l=max(c+1,matrix[a][b].l);if(matrix[a][b].l>matrix[a][b].c) flag=true;}if(!a && !b){for(int i=1;i<=m;i++)for(int j=1;j<=n;j++){matrix[i][j].l=max(c+1,matrix[i][j].l);if(matrix[i][j].l>matrix[i][j].c) flag=true;}}if(!a && b){for(int i=1;i<=m;i++){matrix[i][b].l=max(c+1,matrix[i][b].l);if(matrix[i][b].l>matrix[i][b].c) flag=true;}}if(a && !b){for(int i=1;i<=n;i++){matrix[a][i].l=max(c+1,matrix[a][i].l);if(matrix[a][i].l>matrix[a][i].c) flag=true;}}}if(str[0]=='='){if(a && b){if(c<=matrix[a][b].c && c>=matrix[a][b].l) matrix[a][b].c=matrix[a][b].l=c;else flag=true;}if(!a && !b){for(int i=1;i<=m;i++)for(int j=1;j<=n;j++){if(c<=matrix[i][j].c && c>=matrix[i][j].l) matrix[i][j].c=matrix[i][j].l=c;else flag=true;}}if(!a && b){for(int i=1;i<=m;i++){if(c<=matrix[i][b].c && c>=matrix[i][b].l) matrix[i][b].c=matrix[i][b].l=c;else flag=true;}}if(a && !b){for(int i=1;i<=n;i++){if(c<=matrix[a][i].c && c>=matrix[a][i].l) matrix[a][i].c=matrix[a][i].l=c;else flag=true;}}}}if(flag){printf("IMPOSSIBLE\n");return ;}for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){l[i]-=matrix[i][j].l;l[j+m]+=matrix[i][j].l;add(i,j+m,matrix[i][j].c-matrix[i][j].l);}}for(int i=1;i<=m;i++){l[i]+=matrix[i][0].l;l[0]-=matrix[i][0].l;add(0,i,0);}for(int i=1;i<=n;i++){l[i+m]-=matrix[0][i].l;l[n+m+1]+=matrix[0][i].l;add(i+m,m+n+1,0);}st=n+m+2,ed=n+m+3;vn=n+m+4;int full=0;int maxflow=0;for(int i=0;i<=n+m+1;i++){if(l[i]>0){add(st,i,l[i]);full+=l[i];}elseadd(i,ed,-l[i]);}add(0,n+m+1,INF);add(n+m+1,0,INF);memset(gap,0,sizeof(gap)),memset(dis,0,sizeof(dis));gap[0]=vn;while(dis[st]<vn)maxflow+=isap(st,INF);if(maxflow!=full){printf("IMPOSSIBLE\n");return ;}for(int i=1;i<=m;i++){for(int j=1;j<=n;j++){printf("%d",e[((i-1)*n+j-1)*2^1].c+matrix[i][j].l);if(j!=n)printf(" ");}printf("\n");}}int main(){int t;bool flag=false;scanf("%d",&t);while(t--){if(flag)printf("\n");flag=true;solve();}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.