Cod sursa(job #1911191)

Utilizator derz223Adam Alexandru derz223 Data 7 martie 2017 19:42:33
Problema Flux maxim Scor 50
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f

using namespace std;

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

int n, m, i;

int residue[2000][2000];

bool bfs(vector<list<pair<int, int> > >adjacencyList, int s, int t, int parent[]){

    vector<bool>visited(n+1, false);

    queue<int> q;

    visited[s] = true;
    q.push(s);

    parent[s] = -1;

    while(!q.empty()){

        int u = q.front();
        q.pop();

        for(pair<int, int>aux: adjacencyList[u]){

            int v = aux.first;

            if(!visited[v] && residue[u][v] > 0){

                q.push(v);
                parent[v] = u;
                visited[v] = true;

            }

        }

    }

    return (visited[t] == true);

}

int main()
{

    f>>n>>m;

    vector<list<pair<int, int> > >adjacencyList(n+1);



    for(i=1; i<=m; i++){

        int x, y, z;

        f>>x>>y>>z;

        adjacencyList[x].push_back(make_pair(y, z));

        residue[x][y] = z;

    }

    int parent[n+1];
    int max_flow = 0;

    int v, u;

    while(bfs(adjacencyList, 1, n, parent)){

        int path_flow = INF;

        for(v=n; v!=1; v=parent[v]){
            u = parent[v];
            path_flow = min(path_flow, residue[u][v]);
        }

        for (v=n; v != 1; v=parent[v])
        {
            u = parent[v];
            residue[u][v] -= path_flow;
            residue[v][u] += path_flow;

        }

        max_flow += path_flow;

    }

    g<<max_flow;

    return 0;
}