Cod sursa(job #2907932)

Utilizator _andrei4567Stan Andrei _andrei4567 Data 31 mai 2022 22:40:55
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2 kb
///residual graf(tin si update capacitatea fluxului)
#include <fstream>
#include <vector>
#include <queue>
#include <string.h>
#include <bitset>
#include <climits>

using namespace std;

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

const int N = 1000;
int n, m;

int flux [N + 1][N + 1], parent[N + 2];
bool vis[N + 2];

vector <int> g[N + 2];



struct mata
{
    int x, y, flux;
} p;

bool bfs (int nod, int fin)
{
    memset (vis, 0, sizeof(vis));
    memset (parent, 0, sizeof(parent));
    queue <int> q;
    q.push(nod);
    vis[nod] = 1;
    while (!q.empty())
    {
        int x = q.front();
        q.pop();
        for (auto it : g[x])
            if (!vis[it] &&  flux[x][it] > 0)
            {
                if (it == fin)
                {
                    parent[it] = x;
                    return 1;

                }
                q.push(it);
                parent[it] = x;
                vis[it] = 1;
            }

    }
    return 0;
}
int fordfulkerson (int nod, int fin)
{
    int fluxmax = 0;
    while (bfs(1, n))
    {
        for (auto it : g[n])
        {
            if (flux[it][n] < 0 || !vis[it])
                continue;
            parent[n] = it;
            int pathflow = INT_MAX;
            int nodnou;
            for (int i = fin; i != nod; i = parent[i])
                nodnou = parent[i], pathflow = min(pathflow, flux[nodnou][i]);
            for (int i = fin; i != nod; i = parent[i])
            {
                nodnou = parent[i];
                flux[nodnou][i] -= pathflow;
                flux[i][nodnou] += pathflow;
            }
            fluxmax += pathflow;
        }

    }
    return fluxmax;
}
int main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; ++i)
    {
        cin >> p.x >> p.y >> p.flux;
        g[p.x].push_back(p.y);
        g[p.y].push_back(p.x);
        flux[p.x][p.y] = p.flux;
    }
    cout << fordfulkerson(1, n) << '\n';


    return 0;
}