Time Limit: 3000MS | Memory Limit: 65536K | |||
Total Submissions: 3383 | Accepted: 1275 | Special Judge |
Description
Input
Output
Sample Input
4
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
2 2
3 7
4 6
2
0 0 > -100
0 0 < 100
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
#include<cstdio> #include<cstring> #include<algorithm> #define N 225 #define inf 999999999 using namespace std; int n,m,s,t,num,adj[N],dis[N],q[N],b[N][25],c[N][25],ans[N][25]; struct edge { int v,w,pre; edge(){} edge(int vv,int ww,int p){v=vv;w=ww;pre=p;} }e[20005]; void insert(int u,int v,int w) { e[num]=edge(v,w,adj[u]); adj[u]=num++; e[num]=edge(u,0,adj[v]); adj[v]=num++; } int bfs() { int i,x,v,head=0,tail=0; memset(dis,0,sizeof(dis)); dis[s]=1; q[++tail]=s; while(head!=tail) { x=q[head=(head+1)%N]; for(i=adj[x];~i;i=e[i].pre) if(e[i].w&&!dis[v=e[i].v]) { dis[v]=dis[x]+1; if(v==t) return 1; q[tail=(tail+1)%N]=v; } } return 0; } int dfs(int x,int limit) { if(x==t) return limit; int i,v,tmp,cost=0; for(i=adj[x];~i&&cost<limit;i=e[i].pre) if(e[i].w&&dis[x]==dis[v=e[i].v]-1) { tmp=dfs(v,min(limit-cost,e[i].w)); if(tmp) { e[i].w-=tmp; e[i^1].w+=tmp; cost+=tmp; } else dis[v]=-1; } return cost; } int Dinic() { int ans=0; while(bfs()) ans+=dfs(s,inf); return ans; } int main() { int C; scanf("%d",&C); while(C--) { int i,j,k,u,v,w,S,T,sum=0,flag=1,x1,x2,y1,y2,row[205],col[25]; char str[5]; memset(adj,-1,sizeof(adj)); memset(b,0,sizeof(b)); memset(c,0x3c,sizeof(c)); num=0; scanf("%d%d",&n,&m); S=0; T=n+m+1; s=T+1; t=s+1; for(i=1;i<=n;i++) scanf("%d",&row[i]); for(i=1;i<=m;i++) scanf("%d",&col[i]); scanf("%d",&k); while(k--) { scanf("%d%d%s%d",&u,&v,str,&w); x1=x2=u; y1=y2=v; if(!u)x1=1,x2=n; if(!v)y1=1,y2=m; for(i=x1;i<=x2;i++) for(j=y1;j<=y2;j++) { if(str[0]=='=') b[i][j]=c[i][j]=w; else if(str[0]=='>') b[i][j]=max(b[i][j],w+1); else c[i][j]=min(c[i][j],w-1); if(c[i][j]<b[i][j]) flag=0; } } if(!flag) { puts("IMPOSSIBLE"); continue; } for(i=1;i<=n;i++) for(j=1;j<=m;j++) { insert(i,j+n,c[i][j]-b[i][j]); sum+=b[i][j]; if(b[i][j]) { insert(s,j+n,b[i][j]); insert(i,t,b[i][j]); } } insert(T,S,inf); for(i=1;i<=n;i++) { sum+=row[i]; insert(s,i,row[i]); insert(S,t,row[i]); } for(j=1;j<=m;j++) { sum+=col[j]; insert(s,T,col[j]); insert(n+j,t,col[j]); } if(Dinic()<sum) puts("IMPOSSIBLE"); else { for(i=1;i<=n;i++) for(j=adj[i];~j;j=e[j].pre) ans[i][e[j].v-n]=e[j^1].w; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) printf("%d ",ans[i][j]+b[i][j]); puts(""); } } puts(""); } }