The maximum flow problem --FF and Dinic algorithm

// algorithm_FF.cpp: This file contains the "main" function. Program execution will begin and end here.
//

#include "pch.h"


#include <the iostream>
#include <SET>
#include <math.h>
#include <algorithm>

// Max Stream the FF algorithm
// the Matrix representation

#define INF INT_MAX

// N # DEFINE . 5


int D [. 5] [. 5] = {{0,4,6, INF, INF},
                {INF, 0,2, 3, INF},
                {INF, INF, 0, INF,. 4},
             {INF, INF, INF, 0,. 5},
            {INF, INF, INF, INF, 0}};

#define. 6 N
int C [. 6] [. 6] = {{0,8,12, INF, INF, INF},
                INF {, 0, INF, 6,10, INF},
                {INF, 2,0,10, INF, INF},
             {INF, INF, INF, 0, INF,. 8},
            {INF, INF, INF,
            INF {, INF, INF, INF, INF, 0}};



struct {Edge
    int C;
    int F;
};
Edge Edge [N] [N];


int In Flag [N]; // each point is marked
int prev [N]; // who is in front of a node
int delta [N]; // j to the minimum value of the non-stop min (min (All (CF2)), min (F_))
int catena alberghiera [N]; //

void init_edge (int mat [] [N ]) // second dimension begins to mark
{
    for (int I = 0; I <N; I ++)
    {
        for (int J = 0; J <N; J ++)
        {
            IF (MAT ! [I] [J] = 0 and MAT [I] [J] <INF)
            {
                Edge [I] [J] .c = MAT [I] [J];
                Edge [I] [J] = 0 .F ;
            }
        }

    }

}


const int S = 0; // Why is changed
int. 5 T = const;


// the using namespace STD;

void the FF ()
{
    
    the while (. 1)
    {
        Memset (In Flag, 0xFF, the sizeof (In Flag)); // // set to -1 indicates whether the reference is a reference-1. , as reference numeral 0 unchecked, reference numeral 1 is checked
        memset (prev, 0xff, sizeof ( prev)); // // set to -1
        Memset (Delta, 0xFF, the sizeof (Delta)); // set - . 1
        In Flag [S] = 0; //
        PREV [S] = 0;
        Delta [S] = INF;
        int edge_head = -1; // of Delta, uppercase book triangular
        int edge_tail = 0;
        catena alberghiera [edge_tail] = S;


        while (edge_head <edge_tail) // first time 0 <1; 1,3 // BFS search
        {
            edge_head ++; // first time. 1
            int I;
            I = catena alberghiera [edge_head]; // first time s , 0,1,1 @ 2,2 @ 3,3 @ 4,4 @ 5,5 .. / 4,3 // node
            for (int. 1 = J; J <N; J ++)
            {
                IF (In Flag [J] == -1) // not labeled reporter
                {
                    IF (Edge [I] [J] .c <INF and Edge [I] [j] .f <edge [i ] [j] .c) // abutment and the unsaturated
                    {
                        In Flag [J] = 0;
                        PREV [J] = I;
                        Delta [J] STD :: = min (Delta [I ], Edge [I] [J] .c - Edge [I] [J] .F);
                        catena alberghiera [edge_tail ++] = J; // first time edge_tail = 2, chain [2] = 1; / / II: // 3,2 improves memory leaks here

                    }
                    the else IF (Edge [J] [I] .c <INF and Edge [J] [I] .F> 0) //
                    {
                        In Flag [J] = 0;
                        PREV [J] = -i;
                        Delta [J] STD :: = min (Delta [I], Edge [J] [I] .F);
                        catena alberghiera [edge_tail ++] = J; //
                    }

                }
            }
            In Flag [I] =. 1; // represents It has been processed? ? ? ? I do not know

        }

        IF (Delta [T] In Flag == 0 or [T] == -1) // Delta [T] = 0 indicates, can not find a chain augmented i.e. starting points not saturated, flag [t ] = - 1 represents interrupted, not to the t, i.e., the absence of the augmenting path is not a flag (reference numeral not been checked)
            bREAK; // end
        int t = K1;
        int kO = ABS (PREV [K1] );
        int = A Delta [T];
        the while (. 1) back //
        {
            IF (Edge [kO] [K1] .c <INF)
                Edge [kO] [K1] .F + = A;
            the else IF (Edge [ K1] [kO] .c <INF)
                Edge [K1] [kO] .F - = A;
            if (k0 == s)
                break;
            k1 = k0;
            k0 = abs(prev[k1]);
        }
        delta[t] = 0;//新一轮开始



    }

    int f = 0;
    
    for (int j = 1; j < N; j++)
    {    
        if (edge[s][j].f < inf)
            {
                f += edge[s][j].f;
            }
    }
    
    std::cout << "maxflow" << f << std::endl;


}

int main()
{
    int f = 0;
    init_edge(C);
    FF();



    std::cout << "Hello World!\n";
}



/*

#include<vector>
#include <algorithm>
#define maxn 1200
#define INF 2e9
using namespace std;
int i, j, k, n, m, h, t, tot, ans, st, en;
struct node {
    int c, f;
}edge[maxn][maxn];
int flag[maxn], pre[maxn], alpha[maxn], q[maxn], v;
int read() {
    char c; int x; while (c = getchar(), c<'0' || c>'9'); x = c - '0';
    while (c = getchar(), c >= '0'&&c <= '9') x = x * 10 + c - '0'; return x;

}

//两个例子1// 6 9 1 6 1 2 8 1 3 12  2 4 6  2  5 10  3 2  2  3  4 10  4 6 8  5 4 2  5  6 10
//2//5 6 1 5 1 2 4 1 3 6 2 3 2 2 4 3 3 5 4 4 5 5




void bfs() {
    memset(flag, 0xff, sizeof(flag)); memset(pre, 0xff, sizeof(pre)); memset(alpha, 0xff, sizeof(alpha));
    flag[st] = 0; pre[st] = 0; alpha[st] = INF; h = 0, t = 1; q[t] = st;
    while (h < t) {
        h++; v = q[h];
        for (int i = 1; i <= n; i++) {
            if (flag[i] == -1) {
                if (edge[v][i].c < INF&&edge[v][i].f < edge[v][i].c) {
                    flag[i] = 0; pre[i] = v; alpha[i] = min(alpha[v], edge[v][i].c - edge[v][i].f); q[++t] = i;
                }
                else if (edge[i][v].c < INF&&edge[i][v].f>0) {
                    flag[i] = 0; pre[i] = -v; alpha[i] = min(alpha[v], edge[i][v].f); q[++t] = i;
                }
            }
        }
        flag[v] = 1;
    }
}

void Ford_Fulkerson() {
    while (1) {
        bfs();
        if (alpha[en] == 0 || flag[en] == -1) {
            break;
        }
        int k1 = en, k2 = abs(pre[k1]); int a = alpha[en];
        while (1) {
            if (edge[k2][k1].c < INF) edge[k2][k1].f += a;
            else if (edge[k1][k2].c < INF) edge[k1][k2].f -= a;
            if (k2 == st) break;
            k1 = k2; k2 = abs(pre[k1]);
        }
        alpha[en] = 0;
    }
}

void flow() {
    int maxflow = 0;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++) {
            if (i == st && edge[i][j].f < INF) maxflow += edge[i][j].f;
        }
    The printf ( "% D", MaxFlow);
}

int main () {
    int U, V, C, F;
    n-= Read (); m = Read (); ST = Read (); EN = Read ();
    for (int I =. 1; I <= n-; I ++)
        for (int J =. 1; J <= n-; J ++) Edge [I] [J] .c = INF, Edge [I] [J] .F = 0;
    for (int I =. 1; I <= m; I ++) {
        U = Read (); V = Read (); C = Read ();
        Edge [U] [V] .c = C;
    }
    Ford_Fulkerson ( );
    Flow ();
    return 0;
}
* /

// run the program: Ctrl + F5 or debug> "started (not debug)" menu
// debugger: F5 or debug> "start debug" menu

// tips for getting started :
// 1. using solution Explorer window to add / manage files
// 2. using team Explorer window is connected to the source control
// 3. use the output window to view the generated output and other messages
// 4. Use the Error List window to view the error
// 5. Go to "Project"> "Add new item" to create a new code file, or go to the "Project"> "Add Existing Item" to existing code Add files to the project
// 6. future, to open the project again, go to the "file"> "open"> "project" and select the .sln file

Guess you like

Origin www.cnblogs.com/Jonn-Liu/p/11511707.html