sgu 176 Flow construction
链接:http://acm.sgu.ru/problem.php?contest=0&problem=176
题意:有一个加工厂,里面有n台机器,起点为1终点为n。中间的生产环节有货物限制,输入m个环节。每个环节有u,v,z,c四个数字。u表示起始机器,v表示终止机器,如果c为1,那么这条边的流量必须为z。如果c为0,那么流量在[0, z]之间。保证没有自环,没有重边,只有1号机器能够生产货物,n号机器消耗货物,其他机器不能积累货物。问满足条件的情况下,1号机器最少要生产多少货物。
思路: 有源汇有上下界的最小流 1. 增设一个超级源点,一个超级汇点,以同样的方式将原网络转化为没有流量下界限制的新网络。 2. 不连接原先网络的源汇点,并以超级源汇点为起点,终点做一次最大流maxflow1。 3. 此时从汇点向源点连接一条流量为INF的边,再以超级源汇点做一次网络流maxflow2。 4. 如果从源点出发的流均为满流,则有最小流,最小流的值为从汇点流向源点的流量。 * 在判断是否为满流时,条件为 maxflow1 + maxflow2 = 超级源点流出的流量 * 可以理解为第一遍做网络流时并无T->S的边,所以S->T的流量在第一遍时都已尽力往其他边流了. 于是加上T->S这条边后,都是些剩余的流不到其他边的流量. 从而达到尽可能减少T->S这边上的流量的效果,即减小了最终答案.
代码:
/* ID: wuqi9395@126.com PROG: LANG: C++ */ #include<map> #include<set> #include<queue> #include<stack> #include<cmath> #include<cstdio> #include<vector> #include<string> #include<fstream> #include<cstring> #include<ctype.h> #include<iostream> #include<algorithm> using namespace std; #define INF (1 << 30) #define LINF (1LL << 60) #define PI acos(-1.0) #define mem(a, b) memset(a, b, sizeof(a)) #define rep(i, a, n) for (int i = a; i < n; i++) #define per(i, a, n) for (int i = n - 1; i >= a; i--) #define eps 1e-6 #define debug puts("===============") #define pb push_back #define mkp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define POSIN(x,y) (0 <= (x) && (x) < n && 0 <= (y) && (y) < m) typedef long long ll; typedef unsigned long long ULL; const int maxn = 1000; const int maxm = 100000; struct node { int v; // vertex int cap; // capacity int flow; // current flow in this arc int nxt; } e[maxm * 2]; int g[maxn], cnt; int st, ed, n; int S, D; int N, M; int tot[maxn], low[maxm], id[maxm]; void add(int u, int v, int c, int k) { e[++cnt].v = v; e[cnt].cap = c; e[cnt].flow = 0; e[cnt].nxt = g[u]; g[u] = cnt; id[k] = cnt; e[++cnt].v = u; e[cnt].cap = 0; e[cnt].flow = 0; e[cnt].nxt = g[v]; g[v] = cnt; id[0] = 0; } void init() { memset(g, 0, sizeof(g)); cnt = 1; S = 1, D = N, st = N + 1, ed = N + 2; int u, v, z, c; for (int i = 1; i <= M; i++) { scanf("%d%d%d%d", &u, &v, &z, &c); if (c == 0) { add(u, v, z, i); low[i] = 0; } else { low[i] = z; tot[u] -= z; tot[v] += z; } } n = N + 4; } int dist[maxn], numbs[maxn], q[maxn]; void rev_bfs() { int font = 0, rear = 1; for (int i = 0; i <= n; i++) { //n为总点数 dist[i] = maxn; numbs[i] = 0; } q[font] = ed; dist[ed] = 0; numbs[0] = 1; while(font != rear) { int u = q[font++]; for (int i = g[u]; i; i = e[i].nxt) { if (e[i ^ 1].cap == 0 || dist[e[i].v] < maxn) continue; dist[e[i].v] = dist[u] + 1; ++numbs[dist[e[i].v]]; q[rear++] = e[i].v; } } } int maxflow() { rev_bfs(); int u, totalflow = 0; int curg[maxn], revpath[maxn]; for(int i = 0; i <= n; ++i) curg[i] = g[i]; u = st; while(dist[st] < n) { if(u == ed) { // find an augmenting path int augflow = INF; for(int i = st; i != ed; i = e[curg[i]].v) augflow = min(augflow, e[curg[i]].cap); for(int i = st; i != ed; i = e[curg[i]].v) { e[curg[i]].cap -= augflow; e[curg[i] ^ 1].cap += augflow; e[curg[i]].flow += augflow; e[curg[i] ^ 1].flow -= augflow; } totalflow += augflow; u = st; } int i; for(i = curg[u]; i; i = e[i].nxt) if(e[i].cap > 0 && dist[u] == dist[e[i].v] + 1) break; if(i) { // find an admissible arc, then Advance curg[u] = i; revpath[e[i].v] = i ^ 1; u = e[i].v; } else { // no admissible arc, then relabel this vertex if(0 == (--numbs[dist[u]])) break; // GAP cut, Important! curg[u] = g[u]; int mindist = n; for(int j = g[u]; j; j = e[j].nxt) if(e[j].cap > 0) mindist = min(mindist, dist[e[j].v]); dist[u] = mindist + 1; ++numbs[dist[u]]; if(u != st) u = e[revpath[u]].v; // Backtrack } } return totalflow; } int lowbound_minflow() { long long sum = 0; for (int i = 1; i <= N; i++) { if (tot[i] > 0) add(st, i, tot[i], 0), sum += tot[i]; if (tot[i] < 0) add(i, ed, -tot[i], 0); } int dd = maxflow(); add(D, S, INF, M + 1); dd += maxflow(); if (dd != sum) return -1; return e[id[M + 1]].flow; } int main () { scanf("%d%d", &N, &M); init(); int mn = lowbound_minflow(); if (mn == -1) puts("Impossible"); else { printf("%d\n", mn); for (int i = 1; i < M; i++) { if (low[i] == 0) printf("%d ", e[id[i]].flow); else printf("%d ", low[i]); } if (low[M] == 0) printf("%d\n", e[id[M]].flow); else printf("%d\n", low[M]); } return 0; }