[3376] Luo Gu network maximum flow (detailed version)

Title Description

If that, given a network diagram, and the source and sink, the network obtains its maximum flow.

Input Format

The first row contains four positive integers N, M, S, T, respectively, the number of points, there are the number of edges, number source, sink point number.

Next M lines contains three positive integers ui, vi, wi, represents the i ui article there from edge to reach VI, the right side of wi (i.e., the side of the maximum flow rate wi)

Output Format

Line, contains a positive integer, is the maximum flow of the network.

Sample input and output

Input # 1
4 5 4 3
4 2 30
4 3 20
2 3 20
2 1 30
1 3 40
Output # 1
50

Description / Tips

Constraints of time: 1000ms, 128M

Data Scale:

For 30% of the data: N <= 10, M <= 25

For 70% of the data: N <= 200, M <= 1000

To 100% of the data: N <= 10000, M <= 100000

Sample Description:

There are three paths title:

4 -> 2 -> 3, the line 20 can flow through the

4 -> 3, the flow 20 may be prepared by

4 -> 2 -> 1 -> 3, the flow 10 may be prepared by (edge ​​4 -> 2 before the flow has spent 20)

Therefore, the total flow rate of 20 + 20 + 10 = 50. Output 50.

 

Solution: finally barely understand network flow! (I may be too weak ......) The following is a detailed explanation version!

#include<iostream>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstdio>
using namespace std;
const int oo=0x3f3f3f3f;
const int N=10005;
int cnt=1,n,m,s,t,u,v,w;
struct node{
    int next;
    int w;
    int v;
    int u;
}e[2000005];
inthead [N], D [N]; // D represents a depth 
void the Add ( int U, int V, int W) { 
    CNT ++; E [CNT] .W = W; E [CNT] .v = V; 
    E [CNT] .u = U; E [CNT] = .next head [U]; 
    head [U] = CNT; 
} 

// source: only flow out point
 @ sink: the incoming stream only point
 / / traffic: flowing an edge
 @ capacity: one edge for maximum flow flowing
 @ remaining amount of: an edge volume - flow 


BOOL BFS () { 
    Memset (D, - . 1 , the sizeof (D )); 
    Queue < int > Q;
    q.push (S); D [S] = 0 ;
     the while (! {q.empty ())
         int U = q.front (); q.pop (); // normal walking program (the BFS) 
        for ( int head = I [U]; I; I = E [I] .next) {
             int V = E [I] .v;
             IF (D [V] == - . 1 ! && E [I] .W = 0 ) { 
                D [V] = D [U] + . 1 ; // delamination, i.e. the point where the first layers 
                q.push (V); 
            } 
        } 
    } 
    IF (D [T] = -! . 1 ) return  . 1 ;// T is Unicom 
    the else  return  0 ; // sink depth, it indicates the absence of a hierarchical diagram, without augmenting path 
} 

int DFS ( int U, int F) { // the current point reached, and the current flow 
    IF (U == T) return F; // current point reaches the meeting point, to return the current flow 
    for ( int I = head [U]; I; I = E [I] .next) {
         int V = E [ I] .v; 
         IF (D [V] == D [U] + . 1 ! && E [I] .W = 0 ) { // is a hierarchical diagram && remaining amount is not zero (can also flow redistribution)
             // every time the launch of v u v must ensure that the depth of the depth u must be +1 
            intDFS = TS (V, min (E [I] .W, F)); // downwardly augmented 
            IF (TS> 0 ) { // augmented successful 
                E [I] = .w- TS;   // n Save edge 
                E [I ^ . 1 ] + = .W TS; // flanging plus 
                return TS; // the return value of the augmented 
            } 
        } 
    } 
    return  0 ; 
} 

int main () { 
    the freopen ( " 33367.in " , " R & lt " , stdin); 
    The freopen ( " 33367.out " , " W" , Stdout); 
    Scanf ( " % D% D% D% D " , & n-, & m, & S, & T);
     for ( int I = . 1 ; I <= m; I ++ ) { 
        Scanf ( " % D% D % D " , & U, & V, & W); 
        the add (U, V, W); the add (V, U, 0 ); 
    } 
    int ANS = 0 ;
     the while (BFS ()) // until there are no augmenting path stop   
        ANS + dfs = (S, OO); // OO dfs augmented to make the first value e [i] .w, "filled" first path 
    COUT << ANS;
     return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/wuhu-JJJ/p/11297618.html