Cod sursa(job #2842532)

Utilizator vmnechitaNechita Vlad-Mihai vmnechita Data 1 februarie 2022 09:33:57
Problema Flux maxim Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 10.26 kb
/*
Flux maxim
O retea de transport este un graf orientat in care fiecare muchie are asociata o capacitate si o anumita cantitate de flux. Fluxul primit de fiecare muchie trebuie sa fie mai mic sau egal decat capacitatea acesteia. De asemenea, pentru fiecare nod, fluxul care intra in nod trebuie sa fie egal cu cantitatea de flux care iese din nod. Cu alte cuvinte, suma fluxurilor asociate muchiilor care intra intr-un nod trebuie sa fie egala cu suma fluxurilor asociate muchiilor care ies din nod, exceptie facand nodurile speciale S si D, denumite sursa, respectiv, destinatie. Din nodul sursa poate doar iesi flux, in timp ce in nodul destinatie poate doar intra flux. Valoarea fluxului unei astfel retele este egal cu suma fluxului care iese din sursa sau cu suma fluxului care intra in destinatie (cele doua fluxuri sunt egale).

Cerinta 
Dandu-se o retea de transport, in care initial fluxul pe fiecare muchie este 0, sa se calculeze fluxul maxim care poate fi trimis prin aceasta retea.

Date de intrare
Fisierul de intrare maxflow.in va contine pe prima linie 2 numere, N si M, reprezentand numarul de noduri si numarul de muchii din retea. Pe fiecare din urmatoarele M linii, se vor afla cate 3 numere naturale, x, y si z, cu semnificatia ca exista o muchie care porneste de la nodul x, ajunge in nodul y si are capacitatea z.

Date de ieşire
In fisierul de iesire maxflow.out se va afla un singur numar F, reprezentand fluxul maxim ce poate fi trimis prin retea.

Restricţii
2 ≤ N ≤ 1 000
1 ≤ M ≤ 5 000
Nodul 1 este nodul sursa, iar nodul N este nodul destinatie.
Pentru fiecare muchie, capacitatea va fi un numar natural in intervalul [1, 110 000].
Nu exista nici o muchie x y astfel incat x sa fie egal cu N sau y sa fie egal cu 1.
Intre oricare doua noduri x si y exista maxim un arc, însă arcele x -> y şi y -> x pot exista simultan.
In practica, retelele de flux contin adesea un numar mare de noduri vecine cu destinatia. Testele folosite la evaluarea vitezei algoritmului de flux de la aceasta problema au aceeasi proprietate.

IN:
4 5
1 2 3
1 3 5
2 4 6
3 4 4
3 2 3

OUT:
8
*/

/// Dinic's algorithm - Solutie de 100 de puncte (10 teste)
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define MOD 1000000007
#define MAX 1005

using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

vector < int > v[MAX];
vector < int > :: iterator bun[MAX];
int n, nvl[MAX], a[MAX][MAX];
bool gasit;

void nivelare(int nod);
void dfs(int nod, int &minn);

int main()
{
    int m, i, x, y, z, minn, r = 0;

    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> z;
        a[x][y] = z;
        v[x].pb(y), v[y].pb(x);
    }

    for(i = 1; i <= n; i++) bun[i] = begin(v[i]);
    nivelare(1);
    while(nvl[n] != 0)
    {
        gasit = 1;
        while(gasit == 1)
        {
            minn = INT_MAX, gasit = 0;
            dfs(1, minn);
            if(gasit == 1) r += minn;
        }
        for(i = 1; i <= n; i++) nvl[i] = 0, bun[i] = begin(v[i]);
        nivelare(1);
    }

    fout << r;

    return 0;
}

void nivelare(int nod)
{
    queue < int > q;
    q.push(nod), nvl[nod] = 1;
    while(q.empty() == 0)
    {
        nod = q.front(), q.pop();
        for(auto it:v[nod]) if(a[nod][it] > 0 && nvl[it] == 0) nvl[it] = nvl[nod] + 1, q.push(it);
    }
}

void dfs(int nod, int &minn)
{
    int i;

    for(; gasit == 0 && bun[nod] != end(v[nod]); bun[nod]++)
    {
        vector < int > :: iterator it = bun[nod];
        if(a[nod][*it] > 0 && nvl[*it] == nvl[nod] + 1)
        {
            minn = min(minn, a[nod][*it]);
            if(*it != n) dfs(*it, minn);
            else gasit = 1;

            if(gasit == 1) a[nod][*it] -= minn, a[*it][nod] += minn;
        }
    }
}

/// Edmonds-Karp algorithm with Capacity Scaling - Solutie de 70 de puncte (8 teste)
/*
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define MOD 1000000007
#define MAX 1005

using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

vector < int > v[MAX];
pair < int, int > a[MAX][MAX];
int n, minn, r, prag, prec[MAX];
bool gasit, viz[MAX];

void bfs(int nod);

int main()
{
    int m, i, x, y, z, maxx = 0;

    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> z;
        a[x][y].second = z, maxx = max(maxx, z);
        v[x].pb(y), v[y].pb(x);
    }

    prag = (1<<int(log2(maxx)));
    while(prag != 0)
    {
        for(i = 1; i <= n; i++) viz[i] = 0;
        minn = INT_MAX, gasit = 0, prec[1] = 0;
        bfs(1);
        if(gasit == 1) r += minn;
        else prag /= 2;
    }

    fout << r;

    return 0;
}

void bfs(int nod)
{
    int i;

    queue < int > q;
    q.push(nod), viz[nod] = 1;
    while(q.empty() == 0 && gasit == 0)
    {
        nod = q.front(), q.pop();
        for(auto it = begin(v[nod]); gasit == 0 && it != end(v[nod]); it++)
            if(a[nod][*it].second - a[nod][*it].first >= prag && viz[*it] == 0)
            {
                prec[*it] = nod;
                if(*it == n)
                {
                    gasit = 1, i = *it;
                    while(prec[i] != 0) minn = min(minn, a[prec[i]][i].second - a[prec[i]][i].first), i = prec[i];

                    i = *it;
                    while(prec[i] != 0) a[prec[i]][i].first += minn, a[i][prec[i]].first -= minn, i = prec[i];
                }
                else q.push(*it), viz[*it] = 1;
            }
    }
}
*/

/// Edmonds-Karp algorithm - Solutie de 70 de puncte (7 teste)
/*
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define MOD 1000000007
#define MAX 1005

using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

vector < int > v[MAX];
pair < int, int > a[MAX][MAX];
int n, minn, r, prec[MAX];
bool gasit, viz[MAX];

void bfs(int nod);

int main()
{
    int m, i, x, y, z;

    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> z;
        a[x][y].second = z;
        v[x].pb(y), v[y].pb(x);
    }

    gasit = 1;
    while(gasit == 1)
    {
        for(i = 1; i <= n; i++) viz[i] = 0;
        minn = INT_MAX, gasit = 0, prec[1] = 0;
        bfs(1);
        if(gasit == 1) r += minn;
    }

    fout << r;

    return 0;
}

void bfs(int nod)
{
    int i;

    queue < int > q;
    q.push(nod), viz[nod] = 1;
    while(q.empty() == 0 && gasit == 0)
    {
        nod = q.front(), q.pop();
        for(auto it = begin(v[nod]); gasit == 0 && it != end(v[nod]); it++)
            if(a[nod][*it].second - a[nod][*it].first > 0 && viz[*it] == 0)
            {
                prec[*it] = nod;
                if(*it == n)
                {
                    gasit = 1, i = *it;
                    while(prec[i] != 0) minn = min(minn, a[prec[i]][i].second - a[prec[i]][i].first), i = prec[i];

                    i = *it;
                    while(prec[i] != 0) a[prec[i]][i].first += minn, a[i][prec[i]].first -= minn, i = prec[i];
                }
                else q.push(*it), viz[*it] = 1;
            }
    }
}
*/

/// Ford-Fulkerson algorithm with Capacity Scaling - Solutie de 70 de puncte (9 teste)
/*
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define MOD 1000000007
#define MAX 1005

using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

vector < int > v[MAX];
pair < int, int > a[MAX][MAX];
int n, minn, r, prag, prec[MAX];
bool gasit, viz[MAX];

void dfs(int nod);

int main()
{
    int m, i, x, y, z, maxx = 0;

    fin >> n >> m;
    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> z;
        a[x][y].second = z, maxx = max(maxx, z);
        v[x].pb(y), v[y].pb(x);
    }


    prag = (1<<int(log2(maxx)));
    while(prag != 0)
    {
        for(i = 1; i <= n; i++) viz[i] = 0;
        minn = INT_MAX, gasit = 0, prec[1] = 0;
        dfs(1);
        if(gasit == 1) r += minn;
        else prag /= 2;
    }

    fout << r;

    return 0;
}

void dfs(int nod)
{
    int i;

    viz[nod] = 1;
    for(auto it = begin(v[nod]); gasit == 0 && it != end(v[nod]); it++)
        if(a[nod][*it].second - a[nod][*it].first >= prag && viz[*it] == 0)
        {
            prec[*it] = nod;

            if(*it != n) dfs(*it);
            else
            {
                gasit = 1, i = *it;
                while(prec[i] != 0) minn = min(minn, a[prec[i]][i].second - a[prec[i]][i].first), i = prec[i];
            }

            if(gasit == 1)
            {
                a[nod][*it].first += minn;
                a[*it][nod].first -= minn;
            }
        }
}
*/

/// Ford-Fulkerson algorithm - Solutie de 70 de puncte (7 teste)
/*
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pf push_front
#define MOD 1000000007
#define MAX 1005

using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

vector < int > v[MAX];
pair < int, int > a[MAX][MAX];
int n, minn, r, prec[MAX];
bool gasit, viz[MAX];

void dfs(int nod);

int main()
{
    int m, i, x, y, z;

    fin >> n >> m;

    for(i = 1; i <= m; i++)
    {
        fin >> x >> y >> z;
        a[x][y].second = z;
        v[x].pb(y), v[y].pb(x);
    }

    gasit = 1;
    while(gasit == 1)
    {
        for(i = 1; i <= n; i++) viz[i] = 0;
        minn = INT_MAX, gasit = 0, prec[1] = 0;
        dfs(1);
        if(gasit == 1) r += minn;
    }

    fout << r;

    return 0;
}

void dfs(int nod)
{
    int i;

    viz[nod] = 1;
    for(auto it = begin(v[nod]); gasit == 0 && it != end(v[nod]); it++)
        if(a[nod][*it].second - a[nod][*it].first > 0 && viz[*it] == 0)
        {
            prec[*it] = nod;

            if(*it != n) dfs(*it);
            else
            {
                gasit = 1, i = *it;
                while(prec[i] != 0) minn = min(minn, a[prec[i]][i].second - a[prec[i]][i].first), i = prec[i];
            }

            if(gasit == 1)
            {
                a[nod][*it].first += minn;
                a[*it][nod].first -= minn;
            }
        }
}
*/