Cod sursa(job #3038808)

Utilizator Bogdan405Mocrei Bogdan Bogdan405 Data 27 martie 2023 19:58:45
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#include <vector>

using namespace std;

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

struct muchie
{
    int x,y,c;
};

const int Nmax=5e5+5;
int n,m,t[Nmax],flow,c[Nmax],Min;
bool viz[Nmax];
vector <muchie> M;
vector <int> G[Nmax];

void BFS()
{
    int inc,sf,nc,i,j;
    inc=sf=c[1]=viz[1]=1;
    while (inc<=sf)
    {
        nc=c[inc];
        for (i=0;i<G[nc].size();i++)
        {
            j=G[nc][i];
            if (!viz[M[j].y] and M[j].c>0)
            {
                t[M[j].y]=j;
                c[++sf]=M[j].y;
                viz[M[j].y]=1;
            }
        }
        inc++;
    }
}

int main()
{
    int i,x,y,val,nc;
    f >> n >> m;
    for (i=1;i<=m;i++)
    {
        f >> x >> y >> val;
        M.push_back({x,y,val});
        G[x].push_back(M.size()-1);
        M.push_back({y,x,0});
        G[y].push_back(M.size()-1);
    }
    while (1)
    {
        for (i=1;i<=n;i++)
        {
            t[i]=-1;
            viz[i]=0;
        }
        BFS();
        if (t[n]==-1)
            break;
        Min=2e9;
        nc=n;
        while (t[nc]!=-1)
        {
            Min=min(Min,M[t[nc]].c);
            nc=M[t[nc]].x;
        }
        nc=n;
        while (t[nc]!=-1)
        {
            M[t[nc]].c-=Min;
            M[t[nc]^1].c+=Min;
            nc=M[t[nc]].x;
        }
        flow+=Min;
    }
    g << flow;
    return 0;
}