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.
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.
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.
Sample Input
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
Sample Output
2 3 3 3 3 4 IMPOSSIBLE
Source
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
有源汇有上下界的网络流~
注意0的时候表示该行或该列都有该限制!
坑点无数,加边超麻烦……
先按正常的网络流建图,然后从T向S连一条容量为inf的边,就转化为无源汇有上下界的网络流,剩下的详见http://blog.csdn.net/senyelicone/article/details/54140752~
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
#define inf 999999999
int t,n,m,xx,yy,val,fi[1001],ne[1000001],w[1000001],v[1000001],num[1001],cnt,tot1,st,tot,mx[301][301],mn[301][301],dep[1001],totnum,cur[1001];
bool flag;
char s[3];
void add(int u,int vv,int val)
{
w[++cnt]=vv;ne[cnt]=fi[u];fi[u]=cnt;v[cnt]=val;
w[++cnt]=u;ne[cnt]=fi[vv];fi[vv]=cnt;v[cnt]=0;
}
bool bfs()
{
queue<int> q;
memset(dep,-1,sizeof(dep));
q.push(st);dep[st]=0;
while(!q.empty())
{
int k=q.front();q.pop();
for(int i=fi[k];i;i=ne[i])
if(v[i]>0 && dep[w[i]]==-1)
{
dep[w[i]]=dep[k]+1;q.push(w[i]);
}
}
if(dep[tot]==-1) return 0;
return 1;
}
int findd(int u,int vv)
{
if(u==tot) return vv;
int kkz,now=0;
for(int i=fi[u];i;i=ne[i])
if(v[i]>0 && dep[w[i]]==dep[u]+1 && (kkz=findd(w[i],min(v[i],vv-now))))
{
v[i]-=kkz;v[i^1]+=kkz;now+=kkz;
if(now==vv) return now;
}
if(!now) dep[u]=-1;
return now;
}
bool jud(int u,int v,int val)
{
if(val<mn[u][v] || val>mx[u][v]) flag=1;
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);cnt=1;tot1=n+m+1;st=tot1+1;tot=st+1;flag=0;
for(int i=0;i<=300;i++)
for(int j=0;j<=300;j++) mn[i][j]=-1000,mx[i][j]=1000;
memset(fi,0,sizeof(fi));
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++) scanf("%d",&xx),add(0,i,0),num[0]-=xx,num[i]+=xx;
for(int i=1;i<=m;i++) scanf("%d",&xx),add(n+1,tot1,0),num[i+n]-=xx,num[tot1]+=xx;
scanf("%d",&totnum);
while(totnum--)
{
scanf("%d%d%s%d",&xx,&yy,s,&val);
if(xx && yy)
{
if(s[0]=='=')
{
jud(xx,yy+n,val);mn[xx][yy+n]=mx[xx][yy+n]=val;//jud没有加n
}
else if(s[0]=='>') mn[xx][yy+n]=max(mn[xx][yy+n],val+1);
else mx[xx][yy+n]=min(mx[xx][yy+n],val-1);
}
else if(xx && !yy)
{
for(int i=1;i<=m;i++)
if(s[0]=='=') jud(xx,i+n,val),mn[xx][i+n]=mx[xx][i+n]=val;
else if(s[0]=='>') mn[xx][i+n]=max(mn[xx][i+n],val+1);
else mx[xx][i+n]=min(mx[xx][i+n],val-1);
}
else if(!xx && yy)
{
for(int i=1;i<=n;i++)
if(s[0]=='=') jud(i,yy+n,val),mn[i][yy+n]=mx[i][yy+n]=val;
else if(s[0]=='>') mn[i][yy+n]=max(mn[i][yy+n],val+1);
else mx[i][yy+n]=min(mx[i][yy+n],val-1);
}
else
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(s[0]=='=') jud(i,j+n,val),mn[i][j+n]=mx[i][j+n]=val;
else if(s[0]=='>') mn[i][j+n]=max(mn[i][j+n],val+1);
else mx[i][j+n]=min(mx[i][j+n],val-1);
}
}
for(int i=1;i<=n;i++)
if(!flag)
{
for(int j=1;j<=m;j++)
if(mn[i][j+n]>mx[i][j+n])
{
flag=1;break;
}
else
{
add(i,j+n,mx[i][j+n]-mn[i][j+n]);
num[i]-=mn[i][j+n];num[j+n]+=mn[i][j+n];
}
}
if(flag)
{
printf("IMPOSSIBLE\n");if(t) printf("\n");continue;
}
add(tot1,0,inf);
for(int i=0;i<=tot1;i++)
if(num[i]>0) add(st,i,num[i]);
else if(num[i]<0) add(i,tot,-num[i]);
while(bfs()) findd(st,inf);
for(int i=fi[st];i;i=ne[i])
if(v[i]) flag=1;
if(flag)
{
printf("IMPOSSIBLE\n");if(t) printf("\n");continue;
}
int now=n+m+1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
printf("%d ",v[(now<<1)^1]+mn[i][j+n]);
now++;
}
printf("\n");
}
if(t) printf("\n");
}
return 0;
}