好久没A的这么舒畅了。。第一次写居然1A辣。。
Part2:
有源汇上下界可行流
有源汇上下界可行流就是在多了源点和汇点,这样导致除了源点和汇点外的其他店都流量守恒,我们可以从 T 向 S 连一条容量为无穷大的边,保证 S,T 也流量守恒,然后就可以转化为无源汇上下界可行流做啦。
这题的建图很直观,行列建图,每行连源点,每列连汇点,上下界均为 sumi ,然后关于点 (i,j) 的操作就把第 i 行和第 j 列连边,根据操作确定上下界。然后直接跑有源汇上下界可行流就好辣。
#include<iostream>
#include<cstdio>
#include<cstring>
#define inf 1000000007
#define N 305
#define M 10005
using namespace std;
int head[N],cur[N],dis[N],q[N];
int next[M],list[M],key[M];
int n,m,Q,S,T,SS,TT,cnt,tot,total;
int A[N],B[N],l[N][N],h[N][N],d[N];
bool flag;
inline int read()
{
int a=0,f=1; char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
return a*f;
}
inline void insert(int x,int y,int z)
{
next[++cnt]=head[x];
head[x]=cnt;
list[cnt]=y;
key[cnt]=z;
}
inline bool BFS()
{
int t=0,w=1,x;
memset(dis,-1,sizeof(dis));
q[1]=SS; dis[SS]=1;
while (t<w)
{
x=q[++t];
for (int i=head[x];i;i=next[i])
if (key[i]&&dis[list[i]]==-1)
dis[q[++w]=list[i]]=dis[x]+1;
}
return dis[TT]!=-1;
}
int find(int x,int flow)
{
if (x==TT) return flow;
int w,used=0;
for (int i=cur[x];i;i=next[i])
if (key[i]&&dis[list[i]]==dis[x]+1)
{
w=find(list[i],min(key[i],flow-used));
key[i]-=w; key[i^1]+=w; used+=w;
if (key[i]) cur[x]=i;
if (used==flow) return used;
}
if (!used) dis[x]=-1;
return used;
}
inline int dinic()
{
int ans=0;
while (BFS())
{
for (int i=0;i<=TT;i++) cur[i]=head[i];
ans+=find(SS,inf);
}
return ans;
}
inline void update(int x,int y,int type,int num)
{
switch (type)
{
case 1:
l[x][y]=max(l[x][y],num+1);
break;
case 2:
h[x][y]=min(h[x][y],num-1);
break;
case 3:
l[x][y]=max(l[x][y],num);
h[x][y]=min(h[x][y],num);
break;
}
}
int main()
{
int testcase=read();
while (testcase--)
{
n=read(); m=read(); S=0; T=n+m+1; cnt=1; tot=0; SS=T+1; TT=T+2; total=0; flag=1;
memset(head,0,sizeof(head));
memset(l,0,sizeof(l));
memset(d,0,sizeof(d));
for (int i=1;i<=n;i++) A[i]=read();
for (int i=1;i<=m;i++) B[i]=read();
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
h[i][j]=A[i]>B[j]?B[j]:A[i];
Q=read();
while (Q--)
{
int x=read(),y=read(),type;
char opt[5];
scanf("%s",opt);
switch (opt[0])
{
case '>':
type=1;
break;
case '<':
type=2;
break;
case '=':
type=3;
break;
}
int num=read();
if (!x&&!y)
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
update(i,j,type,num);
else if (!x)
for (int i=1;i<=n;i++)
update(i,y,type,num);
else if (!y)
for (int i=1;i<=m;i++)
update(x,i,type,num);
else update(x,y,type,num);
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
d[i]-=l[i][j],d[j+n]+=l[i][j];
for (int i=1;i<=n;i++)
d[i]+=A[i],d[S]-=A[i];
for (int i=1;i<=m;i++)
d[T]+=B[i],d[i+n]-=B[i];
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
insert(i,j+n,h[i][j]-l[i][j]),insert(j+n,i,0);
insert(T,S,inf); insert(S,T,0);
for (int i=S;i<=T;i++)
if (d[i]>0) total+=d[i],insert(SS,i,d[i]),insert(i,SS,0);
else if (d[i]<0) insert(i,TT,-d[i]),insert(TT,i,0);
if (dinic()!=total) {puts("IMPOSSIBLE"); continue;}
else
{
int now=0;
for (int i=1;i<=n;i++,puts(""))
for (int j=1;j<=m;j++)
{
now++;
printf("%d ",key[now<<1|1]+l[i][j]);
}
puts("");
}
puts("");
}
return 0;
}