Cod sursa(job #2810796)

Utilizator OrzataAndreiOrzata Andrei OrzataAndrei Data 30 noiembrie 2021 11:47:20
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.92 kb
#include <iostream>
#include <fstream>
#include <string.h>
#include <climits>
#include <vector>
#include <queue>

using namespace std;

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

const int MAXN = 1005;

int c[MAXN][MAXN], f[MAXN][MAXN], parent[MAXN],visited[MAXN];
vector<int> edges[MAXN], edges_reverse[MAXN];

int n,m,source,sink;
long flow;

bool BFS(vector<int>edges[],vector<int> edges_reverse[] )
{

    memset(visited,0,sizeof(visited));
    memset(h,0,sizeof(h));

    queue<int> Q;
    Q.push(sink);
    visited[sink]=1;
    while(!Q.empty())
    {
        int x = Q.front();
        Q.pop();
        for(auto y :edges[x])
        {
            if(visited[y]==0&&c[x][y]-f[x][y]>0)
            {
                Q.push(y);
                visited[y] = 1;

                parent[y] = x;
                if(y==source)
                    return true;
            }
        }
        for(auto y : edges_reverse[x])
        {
            if(visited[y]==0&&f[y][x]>0)
            {
                Q.push(y);
                visited[y] = 1;

                parent[y] = -x;
                if(y==source)
                    return true;

            }

        }
    }

    return false;

}

void maxflow()
{
    sink=1;
    source=n;
    while(BFS(edges,edges_reverse))
    {
        for(auto node : edges_reverse[source])
            if(f[node][source] != c[node][source] && visited[node])
            {
                parent[source] =node;
                int fmin=INT_MAX;

                int nod = source;
                while( nod!= sink)
                {

                    int aux=parent[nod];
                    if(aux < 0)
                    {
                        aux= - aux;
                        fmin=min(fmin, f[nod][aux]);
                    }
                    else
                    {
                        fmin=min(fmin, c[aux][nod]-f[aux][nod]);

                    }

                    nod = parent[nod];
                    if(nod<0)
                        nod= -nod;
                }


                nod = source;
                while( nod != sink)
                {
                    int  aux = parent[nod];
                    if(aux <0)
                    {
                        aux = - aux;
                        f[nod][aux]-=fmin;

                    }
                    else
                        f[aux][nod]+=fmin;


                    nod = parent[nod];
                    if(nod<0)
                        nod=-nod;

                }

                flow+=fmin;
            }

    }

}
int main()
{

    in>>n>>m;
    for(int i=1; i<=m; i++)
    {
        int x,y,z;

        in>>x>>y>>z;

        edges[x].push_back(y);
        edges_reverse[y].push_back(x);
        c[x][y]=z;
        f[x][y]=0;

    }
    maxflow();
    out<<flow;

    return 0;

}