Cod sursa(job #3378402)

Utilizator Mirc100Mircea Octavian Mirc100 Data 26 noiembrie 2026 11:14:10
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include <vector>
#include <queue>
#include <fstream>
using namespace std;

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

vector<vector<int>> listaRezid;


int rezid [1001][1001];
vector<int> tata;
void updateFlow(int bottleneck,int t){
    int tata_t = tata[t];
    while( tata_t != 0){

            rezid[tata_t][t] -= bottleneck;

            rezid[t][tata_t] += bottleneck;



        t=tata_t;
        tata_t = tata[tata_t];
    }

}

int minBottleneck(int s, int t){
    int bottleneck = 110000;
    int tata_t = tata[t];
    while( tata_t != 0){
        bottleneck = min(bottleneck, rezid[tata_t][t]);
        t=tata_t;
        tata_t = tata[tata_t];
    }
    return bottleneck;
}
bool bfs(int n,int s, int t){
    vector<bool> vizitati(n+1);
    queue<int> q;
    q.push(s);
    vizitati[s] = true;
    while(!q.empty()){
        int v = q.front();
        q.pop();
        for(auto u : listaRezid[v]){
            if(!vizitati[u] && rezid[v][u]> 0){
                vizitati[u] = true;
                q.push(u);
                tata[u] = v;
                if(u == t)
                    return true;
            }
        }

    }
    return false;
}
int main (){
    int m,n,x,y,z;
    int maxFlow = 0;
    fin>> n>>m;
    tata.assign(n+1,0);
    listaRezid.resize(n+1);

    for(int i = 1; i<=m; i++){
        fin>>x>>y>>z;
        listaRezid[x].push_back(y);
        listaRezid[y].push_back(x);
        rezid[x][y] += z;
    }

    while(bfs(n,1,n)){
        int bottleneck = minBottleneck(1,n);
        updateFlow(bottleneck,n);
        maxFlow += bottleneck;
    }

    fout<< maxFlow;
 return 0;
}