Cod sursa(job #3261580)

Utilizator BogdancxTrifan Bogdan Bogdancx Data 6 decembrie 2024 20:25:41
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.03 kb
#include <iostream>
#include <queue>
#include <vector>
#include <fstream>
#include <algorithm>

using namespace std;

vector<vector<int>> graph;
vector<vector<int>> capacity, flow;

vector<int> bfs(int s, int t) 
{
    queue<int> q;
    vector<int> parent(graph.size() + 1, -1);

    q.push(s);
    parent[s] = -2;

    while(!q.empty())
    {
        int crt = q.front();
        q.pop();

        for(int neigh: graph[crt])
        {
            if(parent[neigh] == -1 && capacity[crt][neigh] - flow[crt][neigh] > 0)
            {
                parent[neigh] = crt;
                q.push(neigh);
            }
        }
    }

    if(parent[t] == -1) {
        return {};
    }
    vector<int> path;
    for(int crt = t; crt != -2; crt = parent[crt])
    {
        path.push_back(crt);
    }

    reverse(path.begin(), path.end());
    return path;
}

int max_flow(int s, int t)
{
    int result = 0;
    vector<int> path;
    while(true)
    {
        vector<int> path = bfs(s, t);

        if(path.size() == 0) {
            break;
        }

        int new_flow = 1e9;
        for(int i = 0; i < path.size() - 1; i++) {
            int x = path[i], y = path[i + 1];
            new_flow = min(new_flow, capacity[x][y] - flow[x][y]);
        }

        for(int i = 0; i < path.size() - 1; i++) {
            int x = path[i];
            int y = path[i + 1];

            flow[x][y] += new_flow;
            flow[y][x] -= new_flow;
        }

        result += new_flow;
    }

    return result;
}

int main() 
{
    ifstream f("maxflow.in");
    ofstream g("maxflow.out");
    int n, m;

    f >> n >> m;

    graph.resize(n + 1);
    capacity.resize(n + 1, vector<int>(n + 1, 0));
    flow.resize(n + 1, vector<int>(n + 1, 0));

    for(int i = 0; i < m; i++)
    {
        int a, b, c;

        f >> a >> b >> c;
        
        graph[a].push_back(b);
        graph[b].push_back(a);
        capacity[a][b] = c;
    }

    g << max_flow(1, n);

    return 0;
}