NYOJ 323 & HDU 1532 & POJ 1273 Drainage Ditches (getting started with the largest stream of network streams), nyoj323
Link: click here
Question: give n rivers, m points, and the traffic of each river, and find the maximum traffic from 1 to m points. Idea: The most naked network stream question is to find the largest stream from the source point to the sink point.
The first network stream, while looking at the introduction in the book, knocked on the Code:
The network stream algorithm ford-fulkerson is used.
The data volume of the question is small, and the number of adjacent tables and adjacent matrices can be used
Code:
# Include <ctype. h> // maximum stream entry # include <stdio. h> # include <vector> # include <stdlib. h> # include <string. h >#include <iostream >#include <algorithm> using namespace std; const int maxn = 1101; const int inf = 0x3f3f3f; struct edge {int to, cap, rev ;}; vector <edge> G [maxn]; // The graph's adjacent table represents bool used [maxn]; // access tag 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});} int dfs (int v, int t, int f) {if (v = t) return f; used [v] = true; for (int I = 0; I <G [v]. size (); I ++) {edge & e = G [v] [I]; if (! Used [e. to] & e. cap> 0) {int 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 ;}int max_flow (int s, int t) {int flow = 0; for (;) {memset (used, 0, sizeof (used); int f = dfs (s, t, inf); if (f = 0) return flow; flow + = f ;}} int main () {int n, m, too, capp, revv; while (~ Scanf ("% d", & n, & m) {memset (G, 0, sizeof (G); for (int I = 0; I <n; I ++) {scanf ("% d", & too, & capp, & revv); add_edge (too, capp, revv );} printf ("% d \ n", max_flow (1, m);} return 0 ;}
Implemented using the BFS of the adjacent matrix:
# Include <cstdio >#include <vector >#include <iostream >#include <queue >#include <cstring> using namespace std; const int N = 300; const int MAX = 0x3f3f3f; int map [N] [N]; int flow [N] [N]; int a [N], p [N]; int Ford_fulkerson (int s, int t) {queue <int> qq; memset (flow, 0, sizeof (flow); int f = 0, u, v; while (1) {memset (a, 0, sizeof (a); a [s] = MAX; qq. push (s); while (! Qq. empty () {u = qq. front (); qq. pop (); for (v = 1; v <= t; v ++) {if (! A [v] & map [u] [v]> flow [u] [v]) // find the new node v {p [v] = u; qq. push (v); // record the father of v, add the FIFO queue a [v] = a [u] <map [u] [v]-flow [u] [v]? A [u]: map [u] [v]-flow [u] [v]; // a [v] is the minimum traffic on the s-v path }}if (a [t] = 0) return f; for (int I = t; I! = S; I = p [I]) {flow [I] [p [I]-= a [t]; flow [p [I] [I] + = a [t];} f + = a [t] ;}} int main () {int n, m; while (~ Scanf ("% d", & n, & m) {memset (map, 0, sizeof (map); for (int I = 0; I <n; I ++) {int x, y, z; scanf ("% d", & x, & y, & z ); map [x] [y] + = z;} printf ("% d \ n", Ford_fulkerson (1, m ));}}