Cod sursa(job #2148356)

Utilizator bogdan10bosBogdan Sitaru bogdan10bos Data 1 martie 2018 17:59:47
Problema Flux maxim Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 3.27 kb
#include <bits/stdc++.h>

using namespace std;

#define FILE_IO

typedef pair<int, int> pii;
typedef pair<pii, int> p3i;

class EdmondsKarp
{
public:
    int N, S, T;
    vector <p3i> edges;
    int cp[1005][1005];
    int flw[1005][1005];
    vector <int> edg[1005];
    int d[1005], f[1005];
    bool initGraph;

    EdmondsKarp()
    {
        N = S = T = 0;
        memset(cp, 0, sizeof(cp));
        memset(flw, 0, sizeof(flw));
        memset(d, 0, sizeof(d));
        memset(f, 0, sizeof(f));
        initGraph = false;
    }

    void addEdge(int x, int y, int c)
    {
        edges.push_back({{x, y}, c});
        initGraph = false;
    }

    void createGraph()
    {
        memset(cp, 0, sizeof(cp));

        N = 0;
        for(auto e: edges)
        {
            N = max(N, max(e.first.first, e.first.second));
            cp[e.first.first][e.first.second] += e.second;
        }

        for(int i = 1; i <= N; i++) edg[i].clear();
        for(int i = 1; i <= N; i++)
            for(int j = i + 1; j <= N; j++)
                if(cp[i][j] || cp[j][i])
                {
                    edg[i].push_back(j);
                    edg[j].push_back(i);
                }

        initGraph = true;
    }

    void BFS(int start)
    {
        for(int i = 1; i <= N; i++) d[i] = 0;
        d[start] = 1;
        queue <int> q;
        q.push(start);
        while(!q.empty())
        {
            int nod = q.front(); q.pop();
            for(auto nxt: edg[nod])
                if(!d[nxt] && cp[nod][nxt] - flw[nod][nxt])
                {
                    d[nxt] = 1 + d[nod];
                    f[nxt] = nod;
                    q.push(nxt);
                }
        }
    }

    int getFlow(int nod)
    {
        if(nod == S)    return (1 << 30);
        int ans = min( cp[ f[nod] ][nod] - flw[ f[nod] ][nod], getFlow(f[nod]) );
        return ans;
    }

    void pushFlow(int nod, int nxt, int flow)
    {
        flw[nod][nxt] += flow;
        flw[nxt][nod] -= flow;
        if(nod == S)    return;
        pushFlow(f[nod], nod, flow);
    }

    int getMaxFlow(int _S, int _T)
    {
        S = _S; T = _T;

        if(!initGraph)  createGraph();

        int flow = 0;
        bool newFlow = true;
        while(newFlow)
        {
            newFlow = false;
            BFS(S);

            for(auto nod: edg[T])
                if(cp[nod][T] - flw[nod][T])
                {
                    int addFlow = getFlow(nod);
                    addFlow = min(addFlow, cp[nod][T] - flw[nod][T]);
                    if(addFlow)
                    {
                        pushFlow(nod, T, addFlow);
                        flow += addFlow;
                        newFlow = true;
                    }
                }
        }

        return flow;
    }
};

EdmondsKarp maxFlow;

int main()
{
    #ifdef FILE_IO
    freopen("maxflow.in", "r", stdin);
    freopen("maxflow.out", "w", stdout);
    #endif

    int N, M;
    scanf("%d%d", &N, &M);
    for(int i = 1; i <= M; i++)
    {
        int x, y, c;
        scanf("%d%d%d", &x, &y, &c);
        maxFlow.addEdge(x, y, c);
    }

    int flow = maxFlow.getMaxFlow(1, N);
    printf("%d\n", flow);

    return 0;
}