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 ;
}

POJ2396 Budget的更多相关文章

  1. POJ2396 Budget [有源汇上下界可行流]

    POJ2396 Budget 题意:n*m的非负整数矩阵,给出每行每列的和,以及一些约束关系x,y,>=<,val,表示格子(x,y)的值与val的关系,0代表整行/列都有这个关系,求判断 ...

  2. POJ2396 Budget 【带下界的最大流】

    Budget Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 5962   Accepted: 2266   Special ...

  3. poj2396 Budget&&ZOJ1994 Budget[有源汇上下界可行流]

    Budget Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge We are supposed to make ...

  4. POJ2396:Budget(带下界的网络流)

    Budget Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 8946   Accepted: 3327   Special ...

  5. poj2396 Budget 上下界可行流

    Budget:http://poj.org/problem?id=2396 题意: 给定一个棋盘,给定每一行每一列的和,还有每个点的性质.求一个合理的棋盘数值放置方式. 思路: 比较经典的网络流模型, ...

  6. POJ2396 Budget(有源汇流量有上下界网络的可行流)

    题目大概给一个有n×m个单元的矩阵,各单元是一个非负整数,已知其每行每列所有单元的和,还有几个约束条件描述一些单元是大于小于还是等于某个数,问矩阵可以是怎样的. 经典的流量有上下界网络流问题. 把行. ...

  7. poj2396 Budget(有源汇上下界可行流)

    [题目链接] http://poj.org/problem?id=2396 [题意] 知道一个矩阵的行列和,且知道一些格子的限制条件,问一个可行的方案. [思路] 设行为X点,列为Y点,构图:连边(s ...

  8. 【POJ2396】Budget(上下界网络流)

    Description We are supposed to make a budget proposal for this multi-site competition. The budget pr ...

  9. 【poj2396】 Budget

    http://poj.org/problem?id=2396 (题目链接) 题意 给出一个矩阵,给出每一行每一列的和,以及若干限制条件,限制了其中每一个元素的上下界,求一种可行的方案使得每一行每一列数 ...

随机推荐

  1. Play Framework 完整实现一个APP(八)

    创建Tag标签 1.创建Model @Entity @Table(name = "blog_tag") public class Tag extends Model impleme ...

  2. MySQL修改root账号密码

    MySQL数据库中如何修改root用户的密码呢?下面总结了修改root用户密码的一些方法   1: 使用set password语句修改 mysql> select user(); +----- ...

  3. 深入理解SQL的四种连接-左外连接、右外连接、内连接、全连接(转)

    1.内联接(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 stude ...

  4. Unity贴图锯齿

    之前在做一个Unity视频插件,发现渲染上去的贴图锯齿十分明显,开了抗锯齿也没用.最后在一次偶然的机会,发现了原来是贴图FilterMode设置有问题 之前用的是FilterMode.Point,后来 ...

  5. 遇到shell重定向的一个奇怪问题:'消失'的标准输入!

    需求: 把找到的文件逐行输出,然后用rm在许可的情况下删除   前置准备:  $ls rm.sh test1 test2 test3 test4 test5 test6 $cat rm.sh #! / ...

  6. 在Windows Server 2012 R2中搭建SQL Server 2012故障转移集群

    需要说明的是我们搭建的SQL Server故障转移集群(SQL Server Failover Cluster)是可用性集群,而不是负载均衡集群,其目的是为了保证服务的连续性和可用性,而不是为了提高服 ...

  7. 理解 Cinder 架构 - 每天5分钟玩转 OpenStack(45)

    从本节开始我们学习 OpenStack 的 Block Storage Service,Cinder 理解 Block Storage 操作系统获得存储空间的方式一般有两种: 通过某种协议(SAS,S ...

  8. Shell十三问[转]

    Shell十三问 转载于网络,稍加整理. (一) 为何叫做Shell? 我们知道计算机的运作不能离开硬件,但使用者却无法直接对硬件作驱动,硬件的驱动只能透过一个称为"操作系统(Operati ...

  9. Entity Framework Code First反向生成代码

    那些年我们生成的代码 早年,笨点的方法通常都是使用DbFirst先生成cs,然后把CS复制出来做些修改 后台基本上就自己使用T4来写,但是一直也没时间完善成通用的版本 MS官方 提供了EntityFr ...

  10. 数据库SQL基本操作

    1.查询表结构 desc 表名 2.显示当前连接用户 show user 3.查看系统拥有哪些用户 select * from all_users; 4.查询当前用户下所有对象 select * fr ...