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. 解决PKIX:unable to find valid certification path to requested target 的问题

    这两天在twitter服务器上忽然遇到这样的异常: e: sun.security.validator.ValidatorException: PKIX path building failed: s ...

  2. Oracle 11g 单实例安装文档

    这里介绍在Red Hat Enterprise Linux Server release 5.7 (Tikanga)下安装ORACLE 11.2.0.1.0的过程,本文仅仅是为了写这样安装指导文档而整 ...

  3. (转载)SQL Reporting Services (Expression Examples)

    https://msdn.microsoft.com/en-us/library/ms157328(v=SQL.100).aspx Expressions are used frequently in ...

  4. C#--属性详解

    本章讨论属性,它允许源代码用简化语法来调用方法.CLR支持两种属性:无参属性 有参属性.在C#中称有参属性为索引器 无参属性 面向对象设计和编程的重要原则之一就是数据封装,意味着类型的字段永远不应该公 ...

  5. ASP.NET MVC Razor

    Razor是MVC3中才有的新的视图引擎.我们知道,在ASP.NET中,ASPX的视图引擎依靠<%和%>来调用C#指令.而MVC3以后有了一套新的使用@标记的Razor语法,使用起来更灵活 ...

  6. 使用 python 获取 Linux 的 IP 信息(通过 ifconfig 命令)

    我们可以使用 python 代码通过调用 ifconfig 命令来获取 Linux 主机的 IP 相关信息,包括:网卡名称.MAC地址.IP地址等. 第一种实现方式: #!/usr/bin/pytho ...

  7. C# 注册表Regedit读写

    注册表的读写 1.读 public static string GetRegeditData() { //Win10 读写LocalMachine权限,没有访问权限 RegistryKey hkml ...

  8. WSTMall网站系统最新官方版

    WSTMall V1.0是在thinkphp 的经典版本3.2.2基础上进行优化开发的, TP 3.2.2不是thinkphp的一个最新的版本,却是thinkphp最金典的一个版本,正所谓站在巨人的肩 ...

  9. php 获取当前服务器 系统

    引子: 今天遇到一个问题,当执行文件操作是,不同系统之间的命令是不同的 , 所以需要判断当前系统. $is_win = strtoupper(substr(PHP_OS,0,3))==='WIN'?1 ...

  10. 面试问题2:给一个5G的大文件,保存的数据为32位的整型,找到所有出现次数超过两次的数字

    问题描述:给一个5G的大文件,保存的数据为32位的整型,找到所有出现次数超过两次的数字 大数据操作: 解决方法一: 依次遍历文件数据, 开始32二进制清0 每次读取一个数,先和二进制位与,如果为0 则 ...