Cod sursa(job #3261951)

Utilizator raizoSoare Antonio raizo Data 7 decembrie 2024 21:51:02
Problema Flux maxim Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.58 kb
#include <fstream>
#include <vector>
#include <map>
#include <queue>
#include <climits>
using namespace std;

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

int n, m, x, y, c;

void dfs(int current, int flow, map<int, map<int, int>>& residual, vector<int>& path, int& sink_flow, int sink = n) {
    if (current == sink) {
        sink_flow = flow;
    }
    else {
        for (auto item : residual[current]) {
            int next = item.first;
            int val = item.second;
            if (path[next] == -1 && val != 0) {
                path[next] = current;
                int new_flow = min(flow, val);
                dfs(next, new_flow, residual, path, sink_flow);
                if (sink_flow != 0) {
                    break;
                }
            }
        }
    }
}

void bfs(map<int, map<int, int>>& residual, vector<int>& path, int& sink_flow, int source = 1, int sink = n) {
    path[source] = 0;
    queue<pair<int, int>> q;
    q.push({ source, INT_MAX });

    while (!q.empty()) {
        int current = q.front().first;
        int flow = q.front().second;
        q.pop();

        for (auto item : residual[current]) {
            int next = item.first;
            int val = item.second;
            if (path[next] == -1 && val != 0) {
                path[next] = current;
                int new_flow = min(flow, val);
                if (next == sink) {
                    sink_flow = new_flow;
                    return;
                }
                q.push({ next, new_flow });
            }
        }
    }
}

int ford_fulkerson(map<int, map<int, int>>& residual, int source = 1, int sink = n) {
    int maxflow = 0;
    while (true) {
        int flow = 0;
        vector<int> path(n + 1, -1);
        
        //bfs(residual, path, flow);
        dfs(source, INT_MAX, residual, path, flow);
        
        if (flow) {
            maxflow += flow;
            int current = sink;
            while (current != source) {
                int prev = path[current];
                residual[prev][current] -= flow;
                residual[current][prev] += flow;
                current = prev;
            }
        }
        else{
            break;
        }
    }

    return maxflow;
}

int main()
{
    cin >> n >> m;
    map<int, int> aux;
    map<int,map<int, int>> residual;

    for (int i = 0; i < m; i++) {
        cin >> x >> y >> c;
        residual[x][y] = c;
        residual[y][x] = 0;
    }

    int maxflow = ford_fulkerson(residual);

    cout << maxflow << endl;
}