Cod sursa(job #2334103)

Utilizator crastanRavariu Eugen crastan Data 2 februarie 2019 11:10:36
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.39 kb
#include <bits/stdc++.h>
#define MAXN 1000
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n, m, sursa, destin, flux;
vector <int> vecini[1 + MAXN];
int cap[1 + MAXN][1 + MAXN];
int father[1 + MAXN];
int q[1 + MAXN], p, u;
inline bool bfs()
{
    memset(father,0,sizeof(father));
    p=0,u=1;
    q[0]=sursa;
    while(p!=u)
    {
        int node=q[p++];
        if(node!=destin)
            for(auto y:vecini[node])
                if(!father[y]&&cap[node][y])
                    father[y]=node,
                    q[u++]=y;
    }
    return father[destin];
}
int main()
{
    fin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,z;
        fin>>x>>y>>z;
        vecini[x].push_back(y);
        vecini[y].push_back(x);
        cap[x][y]+=z;
    }
    sursa=1;
    destin=n;
    while(bfs())
        for(auto y:vecini[destin])
            if(father[y]&&cap[y][destin])
            {
                father[destin]=y;
                int flow=1000000000;
                for(int i=destin;i!=sursa&&flow;i=father[i])
                    flow=min(flow,cap[father[i]][i]);
                if(flow)
                    for(int i=destin;i!=sursa;i=father[i])
                        cap[father[i]][i]-=flow,
                        cap[i][father[i]]+=flow;
                flux+=flow;
            }
    fout<<flux;
    return 0;
}