Topic Link: Click here
Test instructions: A power network has n points, a NP power station, a NC consumption point, and the rest for a transit. M cable, the transit station neither electricity nor power. Each cable has a maximum capacity.
Idea: Set up a super source point and a super sink point, put all the source points and sinks into each, dinic algorithm implementation.
Note the processing of parentheses.
Code:
#include <math.h> #include <queue> #include <deque> #include <vector> #include <stack># Include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #include < Iostream> #include <algorithm>using namespace std; #define MAX (b) A>b?a:b#define Min (A, b) a>b?b:a# Define MEM (A, B) memset (A,b,sizeof (a)) int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};const double EPS = 1e-6;const double Pi = ACOs ( -1.0); const int MAXN = 505;const int inf=0x3f3f3f3f;struct edge{int to, Cap, rev;}; Vector <edge> G[maxn];int LEVEL[MAXN]; Vertex to source point distance number int ITER[MAXN]; The current arc, on its previous side, does not have to consider the int n,np,nc,m,i,j,u,v,z;void add_edge (int from, int to, int cap) {G[from].push_back (edge) { To, Cap, G[to].size ()}); G[to].push_back (Edge) {from, 0, G[from].size ()-1});} BFS is used to calculate the distance number of all points departing from the source point void BFs (int s) {memset (level,-1, sizeof); Queue<int> que; Level[s] = 0; Que.push (s); while (!Que.empty ()) {int v = que.front (); Que.pop (); for (int i = 0; i < g[v].size (); i++) {Edge &e = G[v][i]; if (E.cap > 0 && level[e.to] < 0) {level[e.to] = Level[v] + 1; Que.push (e.to); }}}}//the current shortest augmented path int dfs (int v, int t, int f) through DFS, {if (v = = t) return F; for (int &i = iter[v]; i < g[v].size (); i++)//Here with reference, subtly modifies the ITER array {edge &e = G[v][i]; if (E.cap > 0 && level[v] < level[e.to])//level[v] < level[e.to] This condition guarantees that the current augmented path is the shortest {i NT D = DFS (e.to, T, Min (f, e.cap)); if (d > 0) {e.cap-=d; G[e.to][e.rev].cap + = D; return D; }}} return 0;} void input () {char str[100]; int A, b, C; int s = n; int t = n + 1; for (int i = 0; i < m; i++) {scanf ("%[^ (]", str);scanf ("(%d,%d)%d", &a, &b, &c); Add_edge (A, B, c); } for (int i = 0; I <np; i++) {scanf ("%[^ (]", str); scanf ("(%d)%d", &a, &c); Add_edge (S, a, c); } for (int i = 0; i < NC; i++) {scanf ("%[^ (]", str); scanf ("(%d)%d", &a, &c); Add_edge (A, t, c); }}int max_flow (int s, int t) {int flow = 0; for (;;) {BFS (s); if (Level[t] < 0) return flow; memset (ITER, 0, sizeof (ITER)); int F; while ((f = DFS (S, T, INF)) > 0) {flow + F; }}}int Main () {while (~scanf ("%d%d%d%d", &n, &NP, &NC, &m)) {char str; for (i = 0; i < MAXN; i++) g[i].clear (); Input (); printf ("%d\n", Max_flow (n, n+1)); } return 0;}
Web Stream Learning note 03&&poj1459 Power network