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. 浅谈c#中的delegate和event了

    一.开篇忏悔 对自己最拿手的编程语言C#,我想对你说声对不起,因为我到现在为止才明白c#中的delegate和event是怎么用的,惭愧那.好了,那就趁着阳光明媚的早晨简单来谈谈delegate和ev ...

  2. Gui系统之View体系(2)---View的setContent

    1.从SetContentView讲起 1.1Activty的setContentView里面的内容 public void setContentView(@LayoutRes int layoutR ...

  3. MS SQL 错误 :17883,严重度: 1,状态: 0

    公司一台老旧的SQL SERVER 2000 数据库,一周内会出现若干次(一次或多次)CPU 持续100%,导致应用程序没有反应的情况,如下图所示: 错误信息如下所示: 日期 2013/7/12 2: ...

  4. C++STL - 模板的其他特性

    之前已经总结过函数模板和类模板了,对于模板还有一些其他的特性,这篇主要介绍这些特性.主要都是一些特殊情况. 模板的其他特性 1.缺省参数 (1)类模板的模板参数可以带有缺省值,实例化该模板时,如果提供 ...

  5. python paramiko 进行文件上传处理

    #!/usr/bin/env python # -*- coding:utf-8 -*- import paramiko import uuid class Ha(object): def __ini ...

  6. Nginx服务安装配置

    1.Nginx介绍 Nginx是一个高性能的HTTP和反向代理服务器,由俄罗斯人开发的,第一个版本发布于2004年10月4日.Nginx由于出色的性能,在世界范围内受到了越来越多人的关注,其特点是占有 ...

  7. 【转载】4412开发板嵌入式QtE应用开发环境搭建

    本文转自迅为iTOP-4412开发板实战教程书籍:http://topeetboard.com QtE应用需要使用开发工具qtcreator,本文介绍qtcreator-3.2.2的安装和使用.1. ...

  8. 迅为最新推出iTOP-6818开发平台无缝支持4418开发板

    iTOP-6818开发板是一款四核ARM 八核开发板与iTOP-4418开发板完全兼容,CPU主频1.4GHz,内存1GB DDR3(2GB可选),存储16GB EMMC,板载千兆以太网,GPS,WI ...

  9. 闪电动画模拟(Dielectric Breakdown Model)附源码

    当两个物体之间存在较大的电势差时会出现放电现象,比如生活中常见的闪电现象,闪电形成的条件就是云层积累了大量负电荷之后与地面之间形成了强大的电势差.目前关于闪电建模的方法比较少,下面介绍一种利用电介击穿 ...

  10. SSH-Hibernate+Struts2+Spring的股票项目整合

    创建项目之前:我们需要导入我们需要的Hibernate和Struts2和Spring的相关架包.(博客自创,如有问题请留言博主,拒绝盗版,支持正版http://www.cnblogs.com/WuXu ...