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 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
 
 
제목: n*m의 행렬, n행의 합, m열의 합,
제약도 있습니다.
xy>==이제 출력 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 
코드:
#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("");
	}
}

좋은 웹페이지 즐겨찾기