| Time Limit: 3000MS | Memory Limit: 65536K | |||
| Total Submissions: 3383 | Accepted: 1275 | Special Judge | ||
Description
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
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
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("");
}
}
496



被折叠的 条评论
为什么被折叠?



