Topic links: POJ-1459 Network Power
The meaning of problems
There NP $ $ power stations, a consumer $ NC $, $ m $ directed edge strip, the upper limit given by the capacity of each power station, each consumer demand cap, the upper limit of the capacity of each edge, the maximum Q flow.
Thinking
Very bare the maximum flow problem, the source connected to the power station side, the right side is the capacity limit, consumers and even to the sink side, the right side is the upper limit of demand, even the rest of the side-by-article given $ m $ edges together to It can be.
Code
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using std::queue; const int INF = 0x3f3f3f3f, N = 110, M = N * N * 2; int head[N], d[N]; int s, t, tot, maxflow; struct Edge { int to, cap, nex; } edge[M]; queue<int> q; void add(int x, int y, int z) { edge[++tot].to = y, edge[tot].cap = z, edge[tot].nex = head[x], head[x] = tot; edge[++tot].to = x, edge[tot].cap = 0, edge[tot].nex = head[y], head[y] = tot; } bool bfs() { memset(d, 0, sizeof(d)); while (q.size()) q.pop(); q.push(s); d[s] = 1; while (q.size()) { int x = q.front(); q.pop(); for (int i = head[x]; i; i = edge[i].nex) { int v = edge[i].to; if (edge[i].cap && !d[v]) { q.push(v); d[v] = d[x] + 1; if (v == t) return true; } } } return false; } int dinic(int x, int flow) { if (x == t) return flow; int rest = flow, k; for (int i = head[x]; i && rest; i = edge[i].nex) { int v = edge[i].to; if (edge[i].cap && d[v] == d[x] + 1) { k = dinic(v, std::min(rest, edge[i].cap)); if (!k) d[v] = 0; edge[i].cap -= k; edge[i^1].cap += k; rest -= k; } } return flow - rest; } void init(int n) { tot = 1, maxflow = 0; s = n, t = n + 1; memset(head, 0, sizeof(head)); } int main() { int n, np, nc, m; while (~scanf("%d %d %d %d", &n, &np, &nc, &m)) { init(n); for (int i = 0, u, v, z; i < m; i++) { scanf(" %*c %d %*c %d %*c %d", &u, &v, &z); add(u, v, z); } for (int i = 0, u, z; i < np; i++) { scanf(" %*c %d %*c %d", &u, &z); add(s, u, z); } for (int i = 0, u, z; i < nc; i++) { scanf(" %*c %d %*c %d", &u, &z); add(u, t, z); } while (bfs()) maxflow += dinic(s, INF); printf("%d\n", maxflow); } return 0; }