Cod sursa(job #2679196)

Utilizator bem.andreiIceman bem.andrei Data 29 noiembrie 2020 22:05:22
Problema Flux maxim Scor 60
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.26 kb
#include <bits/stdc++.h>

using namespace std;
ifstream r("maxflow.in");
ofstream w("maxflow.out");
int n, m, parent[1002], rez[1002][1002];
vector<pair<int,int>>g[1002];
void read(){
    r>>n>>m;
    for(int i=0;i<m;i++){
        int x, y, c;
        r>>x>>y>>c;
        g[x].push_back(make_pair(y, c));
        rez[x][y]=c;
    }
}
bool bfs(){
    bool viz[1002];
    memset(viz, 0, sizeof(viz));
    queue<int>q;
    q.push(1);
    viz[1]=1;
    parent[1]=-1;
    while(q.size()!=0){
        int a=q.front();
        q.pop();
        for(auto it: g[a]){
            if(viz[it.first]==0 && rez[a][it.first]>0){
                q.push(it.first);
                parent[it.first]=a;
                viz[it.first]=true;
            }
        }
    }
    return viz[n];
}
int flux(){
    int flow=0;
    while(bfs()!=0){
        int fluxm=1000000000, nod=n;
        while(nod!=1){
            fluxm=min(fluxm, rez[parent[nod]][nod]);
            nod=parent[nod];
        }
        nod=n;
        while(nod!=1){
            rez[parent[nod]][nod]-=fluxm;
            rez[nod][parent[nod]]+=fluxm;
            nod=parent[nod];
        }
        flow+=fluxm;
    }
    return flow;
}
int main()
{
    read();
    w<<flux();
    return 0;
}