Budget


Time Limit: 5 Seconds      Memory Limit: 32768 KB      Special Judge

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

0 2 > 2 
2 1 = 3 
2 3 > 2 
2 3 < 5

2 2 
4 5 
6 7 

1 1 > 10

Sample Output

2 3 3 
3 3 4

IMPOSSIBLE


Source: Asia 2003, Tehran (Iran), Preliminary

【分析】:

首先建图不难

每一行的和为x,S到每行连[x,x]的边

每一列的和为y,每列到T连[y,y]的边

对于一个点i,j,i行向j列连[l,r]的边

然后正常的有源上下界网络流

还要处理输入自身的矛盾

注意初始化,并且l和r初始为+-1000即可,否则可能爆long long

注意输入是>和<,不能理解为>=和<=

注意数组大小

注意测试数据有负数

注意读完数据,不能读入过程中发现矛盾就退出了,有人说似乎读入每组数据最后要读入一个空行

=========================================

QAQ搞了一下午

还有谁跑的比我快?

data下载:(回归社会)

Select Code
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int Z=300,N=1e4+5,M=1e5+5;
const int inf=2e9;
struct edge{int v,next,cap;}e[M];int tot=1,head[N];
int n,m,k,cas,sum,S,T,SS,TT,in[N],dis[N],dn[Z][Z],up[Z][Z],q[M];bool flag;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void add(int x,int y,int z){
e[++tot].v=y;e[tot].cap=z;e[tot].next=head[x];head[x]=tot;
e[++tot].v=x;e[tot].cap=0;e[tot].next=head[y];head[y]=tot;
}
bool bfs(int S,int T){
memset(dis,-1,sizeof dis);
int h=0,t=1;q[t]=S;dis[S]=0;
while(h!=t){
int x=q[++h];
for(int i=head[x];i;i=e[i].next){
if(e[i].cap&&dis[e[i].v]==-1){
dis[e[i].v]=dis[x]+1;
if(e[i].v==T) return 1;
q[++t]=e[i].v;
}
}
}
return 0;
}
int dfs(int x,int T,int f){
if(x==T) return f;
int used=0,t;
for(int i=head[x];i;i=e[i].next){
if(e[i].cap&&dis[e[i].v]==dis[x]+1){
t=dfs(e[i].v,T,min(e[i].cap,f));
e[i].cap-=t;e[i^1].cap+=t;
used+=t;f-=t;
if(!f) return used;
}
}
if(!used) dis[x]=-1;
return used;
}
int dinic(int S,int T){
int res=0;
while(bfs(S,T)) res+=dfs(S,T,2e9);
return res;
}
void jud(int x,int y,int v){
if(v<dn[x][y]||v>up[x][y]) flag=1;
}
void Cl(){
tot=1;
memset(in,0,sizeof in);
memset(head,0,sizeof head);
for(int i=0;i<=n;i++){
for(int j=0;j<=n+m+1;j++){
dn[i][j]=0;
up[i][j]=30000;
}
}
}
void work(){
n=read();m=read();
while(n>200||m>20||n<=0||m<=0);
S=n+m+1;T=S+1;SS=S+2;TT=S+3;Cl();
for(int i=1,x;i<=n;i++) x=read(),add(S,i,0),in[S]-=x,in[i]+=x;
for(int i=1,x;i<=m;i++) x=read(),add(i+n,T,0),in[i+n]-=x,in[T]+=x;
k=read();char s[3];flag=0;
for(int i=1,x,y,z,f;i<=k;i++){
x=read();y=read();scanf("%s",s);z=read();
f=s[0]=='>'?0:s[0]=='<'?1:2;
if(x&&y){
if(!f){
dn[x][y+n]=max(z+1,dn[x][y+n]);
}
else if(f&1){
up[x][y+n]=min(z-1,up[x][y+n]);
}
else{
jud(x,y+n,z);
dn[x][y+n]=up[x][y+n]=z;
}
}
if(!x&&y){
if(!f){
for(int j=1;j<=n;j++){
dn[j][y+n]=max(z+1,dn[j][y+m]);
}
}
else if(f&1){
for(int j=1;j<=n;j++){
up[j][y+n]=min(z-1,up[j][y+m]);
}
}
else{
for(int j=1;j<=n;j++){
jud(j,y+n,z);
dn[j][y+n]=up[j][y+n]=z;
}
}
}
if(x&&!y){
if(!f){
for(int j=1;j<=m;j++){
dn[x][j+n]=max(z+1,dn[j][y+m]);
}
}
else if(f&1){
for(int j=1;j<=m;j++){
up[x][j+n]=min(z-1,up[j][y+m]);
}
}
else{
for(int j=1;j<=m;j++){
jud(x,j+n,z);
dn[x][j+n]=up[x][j+n]=z;
}
}
}
if(!x&&!y){
if(!f){
for(int j=1;j<=n;j++){
for(int k=1;k<=m;k++){
dn[j][k+n]=max(z+1,dn[j][k+n]);
}
}
}
else if(f&1){
for(int j=1;j<=n;j++){
for(int k=1;k<=m;k++){
up[j][k+n]=min(z-1,up[j][k+n]);
}
}
}
else{
for(int j=1;j<=n;j++){
for(int k=1;k<=m;k++){
jud(j,k+n,z);
dn[j][k+n]=up[j][k+n]=z;
}
}
}
}
}
for(int i=1;i<=n&&!flag;i++){
for(int j=1;j<=m;j++){
if(up[i][j+n]<dn[i][j+n]){flag=1;break;}
add(i,j+n,up[i][j+n]-dn[i][j+n]);
in[i]-=dn[i][j+n];
in[j+n]+=dn[i][j+n];
}
}
if(flag){puts("IMPOSSIBLE");return ;}
add(T,S,inf);sum=0;
for(int i=1;i<=T;i++){
if(in[i]>0) add(SS,i,in[i]),sum+=in[i];
if(in[i]<0) add(i,TT,-in[i]);
}
if(dinic(SS,TT)!=sum){puts("IMPOSSIBLE");return ;}
int now=S;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
printf("%d",dn[i][j+n]+e[now<<1^1].cap);
if(j!=m) putchar(' ');
now++;
}
putchar('\n');
}
}
int main(){
cas=read();
for(;cas--;cas?putchar('\n'):1) work();
return 0;
}

poj2396 Budget&&ZOJ1994 Budget[有源汇上下界可行流]的更多相关文章

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

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

  2. 有源汇上下界可行流(POJ2396)

    题意:给出一个n*m的矩阵的每行和及每列和,还有一些格子的限制,求一组合法方案. 源点向行,汇点向列,连一条上下界均为和的边. 对于某格的限制,从它所在行向所在列连其上下界的边. 求有源汇上下界可行流 ...

  3. 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]

    题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...

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

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

  5. poj2396有源汇上下界可行流

    题意:给一些约束条件,要求算能否有可行流,ps:刚开始输入的是每一列和,那么就建一条上下界相同的边,这样满流的时候就一定能保证流量相同了,还有0是该列(行)对另一行每个点都要满足约束条件 解法:先按无 ...

  6. 算法复习——有源汇上下界可行流(bzoj2396)

    题目: Description We are supposed to make a budget proposal for this multi-site competition. The budge ...

  7. ZOJ1994有源汇上下界可行流

    http://fastvj.rainng.com/contest/236779#problem/G Description: n 行 m 列 给你行和 与 列和 然后有Q个限制,表示特定单元格元素大小 ...

  8. bzoj 2406 矩阵 —— 有源汇上下界可行流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2406 这题,首先把题目那个式子的绝对值拆成两个限制,就成了网络流的上下界: 有上下界可行流原 ...

  9. bzoj千题计划158:bzoj2406: 矩阵(有源汇上下界可行流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2406 设矩阵C=A-B 最小化 C 一行或一列和的最大值 整体考虑一行或者一列的和 二分最大值 这样 ...

随机推荐

  1. JS和JSP的差别

    近期非常多同学在纠结于名词缩写之间的相似性.因此本人也来写一篇,讲讲JS和JSP的差别. SUN首先发展出SERVLET,其功能比較强劲,体系设计也非常先进,仅仅是,它输出HTML语句还是採用了老的C ...

  2. Web服务(Web Service)相关概念

    1.概述 Web服务技术(Web Service )是一种面向服务的架构技术,通过标准的Web协议提供服务,保证不同平台的应用服务能够互相操作. 因为Web服务公布的数据基于XML格式和 SOAP协议 ...

  3. resharper警告 :linq replace with single call to FirstOrDefault

    使用resharper时对linq使用的FirstOrDefault 一直产生一个警告,

  4. 转 多线程 闭锁(Latch) 栅栏(CyclicBarrier)

    java多线程并发系列之闭锁(Latch)和栅栏(CyclicBarrier) 标签: java并发编程 2015-05-28 16:45 2939人阅读 评论(0) 收藏 举报 本文章已收录于: . ...

  5. python开发者通过国内镜像安装pip包

    对于Python开发用户来讲,PIP安装软件包是家常便饭.但国外的源下载速度实在太慢,浪费时间.而且经常出现下载后安装出错问题.所以把PIP安装源替换成国内镜像,可以大幅提升下载速度,还可以提高安装成 ...

  6. 常用有话帧检测技术(VAD)

    作者:桂. 时间:2017-05-31  17:43:22 链接:http://www.cnblogs.com/xingshansi/p/6925355.html 前言 总结一下基本的有话帧检测(Vo ...

  7. Linux下的MySQL主主复制

    为什么,会有mysql的主主复制.因为在一些高可用的环境中,mysql的主从不能满足现实中的一些实际需求.比如,一些流量大的网站数据库访问有了瓶颈,需要负载均衡的时候就用两个或者多个的mysql服务器 ...

  8. Python黑魔法,一行实现并行化

    Python 在程序并行化方面多少有些声名狼藉.撇开技术上的问题,例如线程的实现和 GIL,我觉得错误的教学指导才是主要问题.常见的经典 Python 多线程.多进程教程多显得偏“重”.而且往往隔靴搔 ...

  9. Nginx、PCRE和中文URL(UTF8编码)rewrite路径重写匹配问题

    最近遇到了使用Nginx 重写中文UTF8编码路径的问题. 才发现默认情况下Nginx的rewrite是不支持UTF8匹配的. 比如: rewrite ^/(..)$ /2个字符文章.html bre ...

  10. 使用nio实现web服务器

    package com.nio; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocket ...