poj 2396 Budget (有源汇有上下界的网络流)

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

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

Tehran 2003 Preliminary

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:给出一个矩阵每行每列的和,然后给出每个格点的限制条件,求是否存在合法的矩阵。

题解:有源汇有上下界的网络流

对于流量有上下界的有源汇网络,原图中存在源点S和汇点T,为了求可行流,先将其转换为无源汇网络。 
从T-->S引入一条边,其流量上下界为[0, INF],此时原图变为了无源汇网络,然后按照无源汇网络求解可行流的方法来求解。 

简单说一下这个题的原图

l[i]表示第i行,第i行的总和为suml[i];  r[i]表示第i列,第i列的总和为sumr[i]

s->l[i] 流量限制为[suml[i],suml[i]]

r[i]->t 流量限制为[sumr[i],sumr[i]]

一个点(x,y)设限制的范围为[b,c]我们将他转换成一条l[x]->r[y] 流量限制为[b,c]的边。

然后将原图按照有源汇有上下界的网络流的方式重构即可。

#include
#include
#include
#include
#include
#include
#define N 10000
#define inf 1000000000
using namespace std;
int point[N],next[N],v[N],remain[N];
int n,m,tot;
int deep[N],num[N],cur[N],last[N],in[N],out[N],sum[N];
int l[203][203],r[203][203],suml[N],sumr[N],mark[203][203],ck[N];
bool pd;
void init()
{
	tot=-1;
	memset(point,-1,sizeof(point));
	memset(l,0,sizeof(l));
	memset(r,127,sizeof(r));
	memset(num,0,sizeof(num));
	memset(remain,0,sizeof(remain));
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
}
void add(int x,int y,int z)
{
	tot++; next[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=z;
	tot++; next[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
	//cout< p; p.push(t);
	while (!p.empty()){
		int x=p.front(); p.pop();
		for (int i=point[x];i!=-1;i=next[i])
		 if (remain[i^1]&&deep[v[i]]==t)
		  deep[v[i]]=deep[x]+1,p.push(v[i]);
	}
}
void isap(int s,int t)
{
	bfs(s,t);
	for (int i=1;i<=t;i++) num[deep[i]]++;
	for (int i=1;i<=t;i++) cur[i]=point[i];
	int ans=0; int now=s;
	while (deep[s]=l[x][y]&&k<=r[x][y]) l[x][y]=r[x][y]=k;
   	else pd=false;
   	if (s[1]=='<') r[x][y]=min(r[x][y],k-1);
   	if (s[1]=='>') l[x][y]=max(l[x][y],k+1);
}
bool check()
{
	for (int i=2;i<=n+m+1;i++)
	 if (sum[i]<0)
	  if (abs(sum[i])!=remain[ck[i]]) return false;
	return true;
}
int main()
{
   freopen("a.in","r",stdin);
   int T;
   scanf("%d",&T);
   for (int t1=1;t1<=T;t1++) {
   	 scanf("%d%d",&n,&m);
   	 for (int i=1;i<=n;i++) scanf("%d",&suml[i]);
   	 for (int j=1;j<=m;j++) scanf("%d",&sumr[j]);
   	 int s=1; int t=n+m+2;
   	 int q; scanf("%d",&q);
   	 init();
   	 pd=true;
   	 for (int i=1;i<=q;i++) {
   	   int 	x,y,k; char s[2];
   	   scanf("%d%d%s%d",&x,&y,s+1,&k);
   	   if (x&&y) update(x,y,s,k);
	    if (!x&&y) 
	    	for (int j=1;j<=n;j++)
	    	 update(j,y,s,k);
		if (x&&!y)
		    for (int j=1;j<=m;j++)
		     update(x,j,s,k);
		if (!x&&!y)
		 for (int j=1;j<=n;j++)
		  for (int k1=1;k1<=m;k1++)
		   update(j,k1,s,k);
	 }
	 int ss=t+1; int tt=ss+1;
	 for (int i=1;i<=n;i++)
	  for (int j=1;j<=m;j++) {
	  	 //r[i][j]=min(r[i][j],suml[i]);
	  	 //cout<0) add(i+1,tt,sum[i+1]);
	 	else add(ss,i+1,-sum[i+1]);
	 	ck[i+1]=tot;
	 }
	 for (int j=1;j<=m;j++) {
	 	sum[j+n+1]=out[j+n+1]-in[j+n+1];
	 	if (sum[j+n+1]>0) add(j+n+1,tt,sum[j+n+1]);
	 	else add(ss,j+n+1,-sum[j+n+1]);
	 	ck[j+n+1]=tot;
	 }
	 //for (int i=2;i<=n+m+1;i++) cout<



你可能感兴趣的:(网络流)