POJ2396 Budget [有源汇上下界可行流]

POJ2396 Budget

题意:n*m的非负整数矩阵,给出每行每列的和,以及一些约束关系x,y,>=<,val,表示格子(x,y)的值与val的关系,0代表整行/列都有这个关系,求判断是否有解并求一组解


建图显然

\[s \rightarrow _{[行和,行和]} x \rightarrow _{格子(x,y)的限制[l,r]} y \rightarrow_{[列和,列和]} t \]

有源汇上下界可行流
注意是非负整数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define fir first
#define sec second
typedef long long ll;
const int N=2005, M=4e5+5, INF=1e9;
inline ll read(){
    char c=getchar();ll x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

int n, m, x, extra[N], s, t, tot, a[205][205]; 
pair<int, int> g[205][205];
char op[3];
inline bool addCons(int x, int y, char c, int val) {
	pair<int, int> &now = g[x][y];
	if(c == '=') {
		if(now.fir <= val && val <= now.sec) now = make_pair(val, val);
		else return false;
	} else if(c == '<') {
		val--;
		if(now.fir <= val) now.sec = min(now.sec, val);
		else return false;
	} else {
		val++;
		if(now.sec >= val) now.fir = max(now.fir, val);
		else return false;
	}
	return true;
}

struct edge{int v, c, f, ne, lower;}e[M];
int cnt=1, h[N];
inline int ins(int u, int v, int c, int b=0) {
	e[++cnt]=(edge){v, c, 0, h[u], b}; h[u]=cnt;
	e[++cnt]=(edge){u, 0, 0, h[v], b}; h[v]=cnt;
	return cnt-1;
}
int q[N], head, tail, vis[N], d[N], cur[N];
bool bfs(int s, int t) {
	memset(vis, 0, sizeof(vis));
	head=tail=1;
	q[tail++]=s; d[s]=0; vis[s]=1;
	while(head!=tail) {
		int u=q[head++];
		for(int i=h[u];i;i=e[i].ne) 
			if(!vis[e[i].v] && e[i].c>e[i].f) {
				vis[e[i].v]=1; d[e[i].v]=d[u]+1;
				q[tail++]=e[i].v;
				if(e[i].v == t) return true;
			}
	}
	return false;
}
int dfs(int u, int a, int t) { 
	if(u==t || a==0) return a;
	int flow=0, f;
	for(int &i=cur[u];i;i=e[i].ne) 
		if(d[e[i].v]==d[u]+1 && (f=dfs(e[i].v, min(a, e[i].c-e[i].f), t))>0) {
			flow+=f;
			e[i].f+=f;
			e[i^1].f-=f;
			a-=f;
			if(a==0) break;
		}
	if(a) d[u]=-1;
	return flow;
}
int dinic(int s, int t) {
	int flow=0;
	while(bfs(s, t)) {
		for(int i=0; i<=tot; i++) cur[i]=h[i];
		flow+=dfs(s, INF, t); 
	}
	return flow;
}

int main() {
	freopen("in","r",stdin);
	int T=read();
	while(T--) {
		n=read(); m=read(); s=0; t=n+m+1;
		cnt=1; memset(h,0,sizeof(h)); memset(extra, 0, sizeof(extra));
		for(int i=1; i<=n; i++) x=read(), ins(s, i, 0, x), extra[s]-=x, extra[i]+=x;
		for(int i=1; i<=m; i++) x=read(), ins(n+i, t, 0, x), extra[n+i]-=x, extra[t]+=x;
		for(int i=1; i<=n; i++)
			for(int j=1; j<=m; j++) g[i][j] = make_pair(0, INF);
		int cons=read(), flag=1;
		while(cons--) {
			int x=read(), y=read(); scanf("%s",op); int val=read();
			if(x==0 && y==0) 
				for(int i=1; i<=n; i++) for(int j=1; j<=m; j++) flag &= addCons(i, j, op[0], val);
			else if(x==0) for(int i=1; i<=n; i++) flag &= addCons(i, y, op[0], val);
			else if(y==0) for(int j=1; j<=m; j++) flag &= addCons(x, j, op[0], val);
			else flag &= addCons(x, y, op[0], val);
		}
		if(!flag) puts("IMPOSSIBLE");
		else {
			for(int i=1; i<=n; i++)
				for(int j=1; j<=m; j++) {
					int u=i, v=j+n, b=g[i][j].fir, c=g[i][j].sec;
					a[i][j]=ins(u, v, c-b, b); extra[u]-=b; extra[v]+=b;
				}
			ins(t, s, INF);
			int ss=t+1, tt=t+2, sum=0; tot=t+2;
			for(int i=s; i<=t; i++) {
				if(extra[i]>0) ins(ss, i, extra[i]), sum+=extra[i];
				if(extra[i]<0) ins(i, tt, -extra[i]);
			}
			int flow=dinic(ss, tt);
			if(flow != sum) puts("IMPOSSIBLE");
			else {
				for(int i=1; i<=n; i++)
					for(int j=1; j<=m; j++) printf("%d%c",e[a[i][j]].f + e[a[i][j]].lower, j==m?'\n':' ');
			}
		}
		puts("");
	}
}

posted @ 2017-03-28 21:43  Candy?  阅读(303)  评论(0编辑  收藏  举报