Cod sursa(job #2692692)

Utilizator CraniXortDumitrescul Eduard CraniXort Data 3 ianuarie 2021 15:07:42
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.81 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <queue>

FILE *fin, *fout;

class Graph{
    private:
        std::vector <std::vector <int>> edge;
        std::vector <std::vector <int>> cap;
        int *parent;
        bool *visited;
        int V, source=0, sink;
    public:
        Graph(int size){
            V = size;
            edge.resize(V+1);
            cap.resize(V+1);
            for(std::vector <int> &line: cap)
                line.resize(V+1);
            parent = new int[V+1];
            visited = new bool[V+1];
        };

        void addEdge(int src, int dest, int capacity){
            edge[src].push_back(dest);
            edge[dest].push_back(src);
            cap[src][dest] = capacity;
        }

        bool bfs(){
            std::queue <int> q;
            q.push(source);
            memset(visited, 0, sizeof visited);
            visited[source] = true;
            while(q.empty() == false){
                int node = q.front();
                q.pop();

                if(node == sink) continue;

                for(int next: edge[node]){
                    if(visited[next] == false and cap[node][next] > 0){
                        visited[next] = true;
                        q.push(next);
                        parent[next] = node;
                    }
                }
            }
            return visited[sink];
        }

        int maxFlow(int src, int dest){
            source = src;
            sink = dest;

            int node, max_flow=0, flow;
            while(bfs()){
                
                for(int it: edge[sink]){
                    if(visited[it] == true and cap[it][sink] > 0){
                        parent[sink] = it;
                        flow = 1e9;
            
                        node = sink;
                        while(node != source){
                            flow = std::min(flow, cap[parent[node]][node]);
                            node=  parent[node];
                        }

                        node = sink;
                        while(node != source){
                            cap[parent[node]][node] -= flow;
                            cap[node][parent[node]] += flow;
                            node=  parent[node];
                        }

                        max_flow += flow;
                    }
                    
                }
            }

            return max_flow;
        }

};

int main(){
    fin = fopen("maxflow.in", "r");
    fout = fopen("maxflow.out", "w");

    int V, E;
    fscanf(fin, "%d %d", &V, &E);
    Graph graph(V);
    for(int i=0, src, dest, cap; i<E; i++){
        fscanf(fin, "%d %d %d", &src, &dest, &cap);
        graph.addEdge(src, dest, cap);
    }

    fprintf(fout, "%d\n", graph.maxFlow(1, V));

    fclose(fin);
    fclose(fout);
    return 0;
}