這是一道比較基礎的上下界網絡流了
上下界網絡流的算法完全是參考論文《一種簡易的方法求解流量有上下界的網絡 》 還有 《最大流在信息學競賽中應用的一個模型 》
然後 說的也是比較詳細
具體的解法不再多說了,網絡流的算法一直是比較麻煩,代碼量還大,我看了好久才明白算法是啥意思。
剛開始比較迷惑為啥加了一對源匯了,又加一對。 後來明白了,對本題而言,剛開始是一個有源匯的網絡,
就是我們加的第一對的源匯,而我們要轉化為一個無源匯的網絡,就在匯到源加一條無窮容量的邊,這樣就滿足了定義的要求,後來再加一對的源匯,才是論文中說的附加源匯
代碼比較肥碩
輸出答案的時候。剛開始覺得挺蛋疼,後來發現邊都是按順序加的,瞬間覺得世界又美好了
我的模板裡,邊裡存的cap表示這條邊還有多少流量可用,flow就是現在已經使用的流量
[cpp]
#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;
}