### 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 would need at least 2000K Rials for food and someone from Sharif Authorities argued they wouldn't use more than 30000K 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 the 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 should 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 line between matrices.

2

2 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

2 3 3
3 3 4

IMPOSSIBLE

### Source

Tehran 2003 Preliminary

### Solution

1. 每一行的和为x，S到每行连[x,x]的边
2. 每一列的和为y，每列到T连[y,y]的边
3. 对于一个点i，j，i行向j列连[l,r]的边
4. 注意处理自身输入的矛盾

### Code

//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#define maxn 350
#define maxm 350
#define maxe 100010
#define INF 1000
//#define DEBUG
using namespace std;
int n,m,cnt,s,t,ss,tt,num,illegal;
int b[maxn][maxm],c[maxn][maxm];

struct edge
{
int to,cap,next;
edge(int to=0,int cap=0,int next=0):to(to),cap(cap),next(next) {}
}e[maxe];

{
char ch;
do
ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-');
if(ch=='-') sign=-1,ch=getchar();
while(ch>='0'&&ch<='9')
{
ch=getchar();
}
}

inline void ins(int u,int v,int w)
{
}

inline void insert(int u,int v,int w)
{ ins(u,v,w);ins(v,u,0); }

inline void judge(int u,int v,int w)
{ if(b[u][v]>w || c[u][v]<w) illegal=1; }

void init()
{
memset(in,0,sizeof(in));
s=0,t=n+m+1,ss=t+1,tt=t+2,cnt=1,illegal=0;
for(register int i=1;i<=n;++i)
for(register int j=1;j<=m;++j)
b[i][j]=-INF,c[i][j]=INF;
}

void build_graph()
{
for(int x,i=1;i<=n;++i)
for(int x,i=1;i<=m;++i)
while(num--)
{
char ch[5];
if(u==0 && v!=0)
for(int i=1;i<=n;++i)
{
if(ch[0]=='<') c[i][v]=min(c[i][v],w-1);
if(ch[0]=='=') judge(i,v,w),b[i][v]=c[i][v]=w;
if(ch[0]=='>') b[i][v]=max(b[i][v],w+1);
}
if(u!=0 && v==0)
for(int i=1;i<=m;++i)
{
if(ch[0]=='<') c[u][i]=min(c[u][i],w-1);
if(ch[0]=='=') judge(u,i,w),b[u][i]=c[u][i]=w;
if(ch[0]=='>') b[u][i]=max(b[u][i],w+1);
}
if(u==0 && v==0)
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
if(ch[0]=='<') c[i][j]=min(c[i][j],w-1);
if(ch[0]=='=') judge(i,j,w),b[i][j]=c[i][j]=w;
if(ch[0]=='>') b[i][j]=max(b[i][j],w+1);
}
if(u!=0 && v!=0)
{
if(ch[0]=='<') c[u][v]=min(c[u][v],w-1);
if(ch[0]=='=') judge(u,v,w),b[u][v]=c[u][v]=w;
if(ch[0]=='>') b[u][v]=max(b[u][v],w+1);
}
}
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
{
if(b[i][j]>c[i][j]) illegal=1;
insert(i,j+n,c[i][j]-b[i][j]);
in[i]-=b[i][j];
in[j+n]+=b[i][j];
}
insert(t,s,INF);
for(int i=s;i<=t;++i)
{
if(in[i]<0) insert(i,tt,-in[i]);
if(in[i]>0) insert(ss,i,in[i]);
}
}

bool bfs()
{
memset(dis,-1,sizeof(dis));
queue<int> q;
q.push(ss);
dis[ss]=0;
while(!q.empty())
{
int u=q.front();q.pop();
while(i)
{
if(e[i].cap && dis[e[i].to]<0)
{
dis[e[i].to]=dis[u]+1;
q.push(e[i].to);
}
i=e[i].next;
}
}
if(dis[tt]==-1) return 0;
return 1;
}

int dfs(int x,int flow)
{
if(x==tt) return flow;
while(i)
{
if(e[i].cap && dis[e[i].to]==dis[x]+1)
{
w=flow-used;
w=dfs(e[i].to,min(e[i].cap,w));
e[i].cap-=w;
e[i^1].cap+=w;
used+=w;
if(used==flow) return flow;
}
i=e[i].next;
}
if(!used) dis[x]=-1;
return used;
}

bool jud_ill()
{
if(e[i].cap) return 1;
return 0;
}

void solve()
{
if(illegal) { printf("IMPOSSIBLE\n");return; }
while(bfs()) dfs(ss,INF);
if(jud_ill()) { printf("IMPOSSIBLE\n");return; }
else
for(int i=1;i<=n;++i)
{
for(int j=1;j<=m;++j)
{
printf("%d",e[(t<<1)^1].cap+b[i][j]);
if(j!=m) printf(" ");
t++;
}
printf("\n");
}
return ;
}

int main()
{
while(test--)
{
init();
build_graph();
#ifdef DEBUG
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
cout<<"("<<b[i][j]<<","<<c[i][j]<<")"<<endl;
#endif
solve();
}
return 0;
}


Responses