POJ 2396 Budget 有源汇上下界可行流

 
Budget
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 3383 Accepted: 1275 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

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

 
 
题意:给一个n*m的矩阵,n行的和,m列的和,
还有一些约束关系:
x y >=< k 表示 x行y列的数 >=< k(x=0表示整列,y=0表示整行,x=y=0表示整个矩阵)
现在要求一个矩阵满足上述条件,不满足输出IMPOSSIBLE
 
解法:这就是一个有源汇上下界可行流问题。
先说这个问题的解法:由t到s连一条边,权值inf,转换为无源汇上下界可行流问题。
加入超级源S,超级汇T。
保留原图的边uv,边权为上界c-下界b,
S到v连边,边权为b,
u到T连边,边权为b。
 
再说原图的建立:n行+m列的节点加上源s和汇t。
每个格子xy当做一条边,由x到y连边。
s到n行节点连边,边权为每行的和。
m列节点到t连边,边权为每列的和。
 
我刚开始把每个格子当做节点,TLE。。。o(╯□╰)o
另外出现c<b直接输出IMPOSSIBLE
 
代码:
#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("");
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值