POJ2396 Budget

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 7401   Accepted: 2764   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 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.

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

有源汇有上下界的最大流。

在无源汇有上下界网络流问题中,可以用虚拟源汇,转移最小下界的方式改造图。此处同样可以。

如果从S到T连一条容量为INF的边,那么原容量图也可以看做是无源无汇的图。再虚拟一对源汇ST、ED,根据每个点的度连边,跑Dinic即可。

______

  建立n个点代表行,m个点代表列,从某行到某列的连边代表行列交点格子(好像叫矩阵行列式)。按照题目要求限制出每个格子的最大最小值,即是这条边流量的上下界。

  跑Dinic之后,统计答案即可。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int INF=1e9;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct edge{
int v,nxt,f;
}e[mxn];
int hd[],mct=;
inline void add_edge(int u,int v,int c){
e[++mct].v=v;e[mct].f=c;e[mct].nxt=hd[u];hd[u]=mct;return;
}
inline void insert(int u,int v,int c){
// printf("added:%d to %d :%d\n",u,v,c);
add_edge(u,v,c);add_edge(v,u,);return; }
int n,m,S,T,ST,ED;
int in[];
int d[];
bool BFS(){
memset(d,,sizeof d);
d[ST]=;
queue<int>q;
q.push(ST);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(!d[v] && e[i].f){
d[v]=d[u]+;q.push(v);
}
}
}
return d[ED];
}
int DFS(int u,int lim){
if(u==ED)return lim;
int tmp,f=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(d[v]==d[u]+ && e[i].f){
tmp=DFS(v,min(lim,e[i].f));
e[i].f-=tmp;
e[i^].f+=tmp;
lim-=tmp;
f+=tmp;
if(!lim)return f;
}
}
d[u]=;
return f;
}
int Dinic(){
int res=;
while(BFS())res+=DFS(ST,INF);
return res;
}
bool pd(){
for(int i=hd[ST];i;i=e[i].nxt){
if(e[i].f)return ;//附加边未跑满流,不可行
}
return ;
}
int rsum,csum;
int low[][],up[][];
void init(){
memset(hd,,sizeof hd);
memset(in,,sizeof in);
memset(low,,sizeof low);
memset(up,0x3f,sizeof up);
rsum=csum=;
mct=;
return;
}
bool Build_edge(){
for(int i=;i<=n;i++)
for(int j=;j<=m;j++){
if(low[i][j]>up[i][j])return ;
in[i]-=low[i][j];
in[j+n]+=low[i][j];
insert(i,j+n,up[i][j]-low[i][j]);
}
return ;
}
int ans[][];
bool solve(){
for(int i=;i<=T;i++){
if(in[i]>) insert(ST,i,in[i]);
else if(in[i]<)insert(i,ED,-in[i]);
}
insert(T,S,INF);
// printf("%d\n",Dinic());
Dinic();
if(!pd())return ;
for(int i=;i<=n;i++){
for(int j=hd[i];j;j=e[j].nxt){
int v=e[j].v;
ans[i][v-n]=e[j^].f+low[i][v-n];
}
}
return ;
} int main(){
int i,j;
int Cas=read();
while(Cas--){
n=read();m=read();
int x,y,w;
S=;T=n+m+;//源汇
ST=T+;ED=ST+;//超级源汇
init();
for(i=;i<=n;i++){
x=read();
in[S]-=x; in[i]+=x;
rsum+=x;
}
for(i=;i<=m;i++){
x=read();
in[i+n]-=x; in[T]+=x;
csum+=x;
}
int c=read();char op[];
while(c--){
scanf("%d%d%s%d",&x,&y,op,&w);
int s1=x,t1=x;
int s2=y,t2=y;
if(x==){s1=;t1=n;}
if(y==){s2=;t2=m;}
for(i=s1;i<=t1;i++)
for(j=s2;j<=t2;j++){
switch(op[]){
case '=':{low[i][j]=max(w,low[i][j]);up[i][j]=min(w,up[i][j]);break;}
case '>':{low[i][j]=max(w+,low[i][j]);break;}
case '<':{up[i][j]=min(w-,up[i][j]);break;}
}
}
}
if(rsum!=csum || !Build_edge()){printf("IMPOSSIBLE\n\n");continue;}
if(!solve()){printf("IMPOSSIBLE\n\n");continue;}
for(i=;i<=n;i++){
for(j=;j<=m;j++)
printf("%d ",ans[i][j]);
printf("\n");
}
printf("\n");
}
return ;
}
上一篇:robotframework之去除空格、去掉前面的0、增加空格换行符的方法,两个字符之间的拼接


下一篇:chm手册显示已取消到该网页的导航