Cod sursa(job #2811357)

Utilizator realmeabefirhuja petru realmeabefir Data 1 decembrie 2021 21:35:32
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.02 kb
#include <iostream>
#include <bits/stdc++.h>

#define maxFlow  110000

using namespace std;

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

int n, m;
int s, f;
vector<int> la[1001];
int fluxLeft[1001][1001];
int parent[1001];
int flux = 0;

void getPath()
{
    memset(parent, 0, sizeof(parent));
    vector<int> q;
    parent[s] = -1;
    q.push_back(s);

    for (int i = 0; i < q.size() ; i++)
    {
        if (q[i] == n)
        {
            continue;
        }
        for (auto to: la[q[i]])
        {
            if (fluxLeft[q[i]][to] <= 0 || parent[to] != 0)
                continue;

            parent[to] = q[i];
            q.push_back(to);
        }
    }
}

int main()
{
    in >> n >> m;
    s = 1; f = n;
    for (int i = 1; i <= m ; ++i)
    {
        int x,y,c;
        in >> x >> y >> c;
        la[x].push_back(y);
        la[y].push_back(x);

        fluxLeft[x][y] = c;
    }

    getPath();
    while (parent[f] != 0)
    {
        for (int i = -1; i < (int)la[f].size(); i++)
        {
            if (i != -1)
            {
                parent[f] = la[f][i];
            }

            int minn = maxFlow;
            int nod = f;
            while (parent[nod] != -1)
            {
                if (fluxLeft[parent[nod]][nod] < minn)
                    minn = fluxLeft[parent[nod]][nod];
                if (minn <= 0)
                    break;
                nod = parent[nod];
            }

            if (minn <= 0)
                continue;

            flux += minn;
            nod = f;
            while (parent[nod] != -1)
            {
                fluxLeft[parent[nod]][nod] -= minn;
                fluxLeft[nod][parent[nod]] += minn;
                nod = parent[nod];
            }
        }

        getPath();
    }

    out << flux;

    return 0;
}

/*
6 8
1 2 10
1 3 8
2 3 2
2 4 5
3 5 10
5 4 8
4 6 7
5 6 10
// 15
*/

/*
6 7
1 2 6
1 3 8
2 4 5
3 5 4
2 5 3
4 6 7
5 6 5
// 10
*/