这是一道比较基础的上下界网络流了
上下界网络流的算法完全是参考论文《一种简易的方法求解流量有上下界的网络 》 还有 《最大流在信息学竞赛中应用的一个模型 》
然后http://blog.csdn.net/water_glass/article/details/6823741说的也是比较详细
具体的解法不再多说了,网络流的算法一直是比较麻烦,代码量还大,我看了好久才明白算法是啥意思。
总体来说有上下界的网络流分两种问题,第一种是只求可行流,第二种呢就是给定源汇求最大流。
对于第一种问题,一般就是碰见有源汇的网络,然后给变成无源汇的。
第二种呢就按文中说的吧。
刚开始比较迷惑为啥加了一对源汇了,又加一对。 后来明白了,对本题而言,刚开始是一个有源汇的网络,
就是我们加的第一对的源汇,而我们要转化为一个无源汇的网络,就在汇到源加一条无穷容量的边,这样就满足了定义的要求,后来再加一对的源汇,才是论文中说的附加源汇
代码比较肥硕
输出答案的时候。刚开始觉得挺蛋疼,后来发现边都是按顺序加的,瞬间觉得世界又美好了
我的模板里,边里存的cap表示这条边还有多少流量可用,flow就是现在已经使用的流量
#include <iostream> #include <algorithm> #include <cstring> #include <string> #include <cstdio> #include <cmath> #include <queue> #include <map> #include <set> #define MAXN 555 #define MAXM 555555 #define INF 1000000007 using namespace std; struct node { int ver; // vertex int cap; // capacity int flow; // current flow in this arc int next, rev; }edge[MAXM]; int dist[MAXN], numbs[MAXN], src, des, n; int head[MAXN], e; void add(int x, int y, int c) { //e记录边的总数 edge[e].ver = y; edge[e].cap = c; edge[e].flow = 0; edge[e].rev = e + 1; //反向边在edge中的下标位置 edge[e].next = head[x]; //记录以x为起点的上一条边在edge中的下标位置 head[x] = e++; //以x为起点的边的位置 //反向边 edge[e].ver = x; edge[e].cap = 0; //反向边的初始可行流量为0 edge[e].flow = 0; edge[e].rev = e - 1; edge[e].next = head[y]; head[y] = e++; } void rev_BFS() { int Q[MAXN], qhead = 0, qtail = 0; for(int i = 1; i <= n; ++i) { dist[i] = MAXN; numbs[i] = 0; } Q[qtail++] = des; dist[des] = 0; numbs[0] = 1; while(qhead != qtail) { int v = Q[qhead++]; for(int i = head[v]; i != -1; i = edge[i].next) { if(edge[edge[i].rev].cap == 0 || dist[edge[i].ver] < MAXN)continue; dist[edge[i].ver] = dist[v] + 1; ++numbs[dist[edge[i].ver]]; Q[qtail++] = edge[i].ver; } } } void init() { e = 0; memset(head, -1, sizeof(head)); } int maxflow() { int u, totalflow = 0; int Curhead[MAXN], revpath[MAXN]; for(int i = 1; i <= n; ++i)Curhead[i] = head[i]; u = src; while(dist[src] < n) { if(u == des) // find an augmenting path { int augflow = INF; for(int i = src; i != des; i = edge[Curhead[i]].ver) augflow = min(augflow, edge[Curhead[i]].cap); for(int i = src; i != des; i = edge[Curhead[i]].ver) { edge[Curhead[i]].cap -= augflow; edge[edge[Curhead[i]].rev].cap += augflow; edge[Curhead[i]].flow += augflow; edge[edge[Curhead[i]].rev].flow -= augflow; } totalflow += augflow; u = src; } int i; for(i = Curhead[u]; i != -1; i = edge[i].next) if(edge[i].cap > 0 && dist[u] == dist[edge[i].ver] + 1)break; if(i != -1) // find an admissible arc, then Advance { Curhead[u] = i; revpath[edge[i].ver] = edge[i].rev; u = edge[i].ver; } else // no admissible arc, then relabel this vertex { if(0 == (--numbs[dist[u]]))break; // GAP cut, Important! Curhead[u] = head[u]; int mindist = n; for(int j = head[u]; j != -1; j = edge[j].next) if(edge[j].cap > 0)mindist = min(mindist, dist[edge[j].ver]); dist[u] = mindist + 1; ++numbs[dist[u]]; if(u != src) u = edge[revpath[u]].ver; // Backtrack } } return totalflow; } int low[MAXN][MAXN], up[MAXN][MAXN]; int xj[MAXN]; int col, row, s, t; bool build() { for(int i = 1; i <= row; i++) for(int j = 1; j <= col; j++) { if(low[i][j] > up[i][j]) return false; else { xj[i] -= low[i][j]; xj[j + row] += low[i][j]; add(i, j + row, up[i][j] - low[i][j]); } } return true; } void solve() { src = t + 1; des = t + 2; n = des; for(int i = 1; i <= t; i++) if(xj[i] > 0) add(src, i, xj[i]); else if(xj[i] < 0) add(i, des, -xj[i]); add(t, s, INF); rev_BFS(); maxflow(); for(int i = head[src]; i != -1; i = edge[i].next) if(edge[i].cap > 0) { printf("IMPOSSIBLE\n\n"); return; } for(int i = 1; i <= row; i++) for(int j = 1; j <= col; j++) { printf("%d", edge[((i - 1) * col + j - 1) * 2].flow + low[i][j]); if(j < col) putchar(' '); else putchar('\n'); } printf("\n"); } int main() { int T, u, v, w; char op[5]; scanf("%d", &T); while(T--) { init(); scanf("%d%d", &row, & col); memset(xj, 0, sizeof(xj)); for(int i = 0; i < row + 5; i++) for(int j = 0; j < col + 5; j++) low[i][j] = 0, up[i][j] = INF; s = row + col + 1; t = row + col + 2; for(int i = 1; i <= row; i++) { scanf("%d", &u); xj[s] -= u; xj[i] += u; } for(int i = row + 1; i <= row + col; i++) { scanf("%d", &u); xj[t] += u; xj[i] -= u; } int q, lc, rc, lr, rr; scanf("%d", &q); while(q--) { scanf("%d%d%s%d", &u, &v, op, &w); lr = rr = u; lc = rc = v; if(u == 0) lr = 1, rr = row; if(v == 0) lc = 1, rc = col; for(int i = lr; i <= rr; i++) for(int j = lc; j <= rc; j++) { if(op[0] == '=') low[i][j] = max(low[i][j], w), up[i][j] = min(up[i][j], w); else if(op[0] == '<') up[i][j] = min(w - 1, up[i][j]); else if(op[0] == '>') low[i][j] = max(low[i][j], w + 1); } } if(build()) solve(); else printf("IMPOSSIBLE\n\n"); } return 0; }