POJ 2396 Budget (上下界网络流)
Budget
Time Limit: 3000MS Memory Limit: 65536K
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
题目大意:
有一个n*m的方阵,里面的数字未知,但是我们知道如下约束条件:
每一行的数字的和
每一列的数字的和
某些格子有特殊的大小约束,用大于号,小于号和等于号表示
(表示时 n 或 m 为 0 表示这一行或一列同时满足这个限定)
问:是否存在用正数填充这个方阵的方案,满足所有的约束,若有,输出之,否则输出IMPOSSIBLE。
思路:
先说说怎么处理下界(限流就是上界了)。(对不起又要偷图了)
对于图上的一条下界为k1,上界为k2的边,我们考虑把它变成一个只有上界的边(下界默认为0),转换为普通的网络流,满流时有解。
对于有源有汇的图,我们在原图汇点连向原图源点一条流量为INF的边,形成无源无汇图即可。(这条边的意义在于,能够使得源点会汇点满足成为流量平衡条件的普通节点。)
这种行列关系的题目类似二分图,应该是套路建图了。每行是一个点,每列是一个点,行列之间连边的流量就是一个格点的值(限定了横列)源点连行,流量为这行的和,列连汇点,流量为这列的和。
然后求一次最大流(以虚拟源点和虚拟汇点为起止),如果满流,那么说明这个网络是存在满足所有下界的可行流的。因为这个保证了下界的图可以满足流量平衡,那么它一定就是可行流了。
稍微提一下,此时的可行流(从汇点流向源点的流量)并不一定是最小流,因为满足情况的可行流是不唯一的。
如果要求的话,我们需要要删除掉那条从汇点到源点的INF的边,接下来在残量网络上跑以原图源点和原图汇点为起止最大流即可。最终的最大流流量=可行流流量+新增广出的流量。(如果求最小流的话,就反过来跑以原图汇点和原图源点为起止最大流,最终的最小流流量=可行流流量-新增广出的流量。)
那么会不会增广的时候使得一些边不满足流量下限?
不会的,因为我们一开始建的图就是把大小等于流量下限的流量拿出去之后的残量网络,这些流量根本没有在图中出现,也就不会被更改到。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 300
#define Maxm 50
#define N 400010
const int inf = 0x7fffffff;
int up[Maxn][Maxm], dn[Maxn][Maxm]; // 上下界
struct Edge{
int from, to, next, w;
}ed[N];
int head[400], dis[400], last[400];
int idc, s, t, S, T, n, m, sum, ans;
bool flag;
void adde(int u, int v, int w){
if(w == 0) return; //去除权值为0的边
if(u == S) sum += w; //统计满流条件
ed[++idc].from = u; ed[idc].to = v; ed[idc].w = w;
ed[idc].next = head[u]; head[u] = idc;
ed[++idc].from = v; ed[idc].to = u; ed[idc].w = 0;
ed[idc].next = head[v]; head[v] = idc;
}
void build(int u, int v, int dn, int up){
if(up < dn){ flag = 0; return; }
adde(S, v, dn); adde(u, T, dn);
adde(u, v, up-dn);
}
void init(){
memset(head,0,sizeof(head));
idc = 1; flag = 1; sum = 0;
s = 0; t = n + m + 1; S = t + 1; T = S + 1;
//s,t为真正源汇,S,T为虚拟源汇
for(int i=1; i<=n; i++){
int x; scanf("%d", &x);
build(S, i, x, x);//固定流量,up,down都是x
}
for(int i=1; i<=m; i++){
int x; scanf("%d", &x);
build(i+n, T, x, x);//行:1~n,列n+1~n+m
}
int q; scanf("%d", &q);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++){
dn[i][j] = 0;
up[i][j] = inf;
}
while( q-- ){
int x, y, z; char cc[5];
scanf("%d%d%s%d", &x, &y, cc, &z);
int k1, k2, k3, k4;
if( !x ) k1=1, k2=n;//整行限定
else k1 = k2 = x;
if( !y ) k3=1, k4=m;//整列限定
else k3 = k4 = y;
for(int i=k1; i<=k2; i++)
for(int j=k3; j<=k4; j++){
if(cc[0] == '>') dn[i][j] = max(dn[i][j], z+1);//下界
if(cc[0] == '=') dn[i][j] = max(dn[i][j], z);
if(cc[0] == '<') up[i][j] = min(up[i][j], z-1);//上界(可以取等)
if(cc[0] == '=') up[i][j] = min(up[i][j], z);
}
}
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++){
build(i, j+n, dn[i][j], up[i][j]);
if( !flag ) break;//up<down 不合法
}
build(t, s, 0, inf);//真汇连向真源变成无源无汇,跑最大流
}
int q[600];
int pri[Maxn][Maxm]; //输出矩阵
bool bfs(){
memset(dis, -1, sizeof(dis));
int l=0, r=1;
q[1]=S; dis[S]=0;
while(l < r){
int u = q[++l];
for(int k=head[u]; k; k=ed[k].next) {
int v = ed[k].to;
if(ed[k].w && dis[v] == -1){
dis[v] = dis[u] + 1;
q[++r] = v;
if(v == T) break;//到达
}
}
}
for(int i=0; i<=T; i++){//
last[i] = head[i];
}
return dis[T] != -1;//能否到达
}
int find(int u, int low){
if (u==T || low==0) return low;
int totflow =0;
for(int k=last[u]; k; last[u]=k=ed[k].next){//当前弧优化
int v = ed[k].to;
if(ed[k].w && dis[v] == dis[u] + 1){
int f = find(v, min(low,ed[k].w));
ed[k].w -= f; ed[k^1].w += f;
totflow += f; low -= f;
//if(ed[k].w) last[u]=k;
if(low == 0) return totflow;
}
}
if( !totflow ) dis[u] = -1;//
return totflow;
}
void Dinic(){
ans = 0;
while (bfs()) ans += find(S, inf);
memset(pri, 0, sizeof(pri));
}
int main(){
int T; scanf("%d", &T);
while( T-- ){
scanf("%d%d", &n, &m);
init();
if( !flag ){
printf("IMPOSSIBLE\n\n");
continue;
}
Dinic();
if(ans == sum){//判断是否满流
for(int i=3; i<=idc; i+=2){
if(ed[i].from>n && ed[i].from<=n+m && ed[i].to<=n+m){
//用反向边取值,判断排除 s t S T 之间的边
pri[ed[i].to][ed[i].from-n]=ed[i].w;
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)
printf("%d ",pri[i][j]+dn[i][j]);//记得加上先流的下界
printf("\n");
}
}
else printf("IMPOSSIBLE\n");
printf("\n");
}
return 0;
}
上一篇: 常用设计模式思想和设计原则