Cod sursa(job #3137010)

Utilizator proflaurianPanaete Adrian proflaurian Data 10 iunie 2023 09:38:18
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
const int N = 1001;
const int oo = 200000;
int n,m,cap[N][N],flux[N][N],fluxMax,T[N];
vector<int> v[N];
bool bfs();
int main()
{
    f>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int x,y,c;
        f>>x>>y>>c;
        cap[x][y]=c;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    while(bfs());
    g<<fluxMax<<'\n';
    return 0;
}
bool bfs()
{
    queue<int> q;
    for(int i=1;i<=n;i++)
        T[i]=0;
    T[1]=1;
    q.push(1);
    while(q.size())
    {
        int nod=q.front();
        q.pop();
        for(auto vec:v[nod])
            if(!T[vec]&&cap[nod][vec]>flux[nod][vec])
            {
                T[vec]=nod;
                q.push(vec);
            }
    }
    if(!T[n])
        return false;
    int x=T[n],y=n;
    int fluxNow=oo;
    while(y!=1)
    {
        fluxNow = min(fluxNow,cap[x][y]-flux[x][y]);
        y=T[y];
        x=T[x];
    }
    fluxMax+=fluxNow;
    x=T[n];y=n;
    while(y!=1)
    {
        flux[x][y]+=fluxNow;
        flux[y][x]-=fluxNow;
        x=T[x];
        y=T[y];
    }
    return true;
}