Cod sursa(job #3235948)

Utilizator mihai2003LLL LLL mihai2003 Data 24 iunie 2024 14:06:24
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.96 kb
#include <fstream>
#include <vector>
#include <cstdint>
#include <queue>
#include <memory.h>
#include <iostream>

const int NMAX = 1001;

std::ifstream in("maxflow.in");
std::ofstream out("maxflow.out");

class MaxFlow{
    public:
        MaxFlow(int n) : n(n){}
        uint32_t getFlow(){
            uint32_t flow = 0;
            while(bfs(1,n)){
                int minf = 1e7;
                int aux = n;
                while(parent[aux] != -1){
                    minf = std::min(c[parent[aux]][aux]-f[parent[aux]][aux],minf);
                    aux=parent[aux];
                }
                aux = n;
                while(parent[aux] != -1){
                    f[parent[aux]][aux] += minf;
                    f[aux][parent[aux]] -= minf;
                    aux=parent[aux];
                }
                flow+=minf;
            }
            return flow;
        }

        void add_edge(int a,int b,int cost){
            c[a][b] = cost;
        }
    private:
        int n;
        int32_t f[NMAX][NMAX];
        int32_t c[NMAX][NMAX];
        std::vector<int> g[NMAX];
        int parent[NMAX];
        bool bfs(int start,int finish){
            std::queue<int>q;
            q.push(start);
            memset(parent,0,NMAX*sizeof(int));
            parent[start] = -1;
            while(!q.empty()){
                int aux = q.front();
                q.pop();
                
                for(int x=1;x<=n;x++){
                    if(parent[x] == 0 && c[aux][x] - f[aux][x] > 0){
                        parent[x] = aux;
                        q.push(x);
                       if(x == finish){
                            return 1;
                       }
                    }
                }
            }
            return 0;
        }

};


int main(){
    int n,m;
    in>>n>>m;
    MaxFlow prob(n);
    while(m--){
        int a,b,c;
        in>>a>>b>>c;
        prob.add_edge(a,b,c);
    }
    std::cout<<"Here\n";
    out<<prob.getFlow();
    return 0;
}