Cod sursa(job #2562709)

Utilizator veselin465vesko penev veselin465 Data 29 februarie 2020 17:19:33
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.36 kb
#include<iostream>
#include<vector>
#include<queue>
#include <fstream>

#define MAX_N 10000
#define MAX_INT 100000

using namespace std;

struct Edge{
    int to,weight,toBackIndex;
    bool isOriginal;
};

int n,m,s,t;
vector<Edge> neighbour[MAX_N];

int main(){

    ifstream input;
    input.open("maxflow.in");

    ofstream output;
    output.open("maxflow.out");

    input>>n>>m;



    for(int i=0;i<n;i++){
        neighbour[i]=vector<Edge>();
    }

    for(int i=0;i<m;i++){
        int a,b,w;
        input>>a>>b>>w;

        int fromBackIndex = neighbour[a].size();
        int toBackIndex = neighbour[b].size();

        Edge e = Edge();

        e.to = b;
        e.weight = w;
        e.toBackIndex = toBackIndex;
        e.isOriginal=true;
        neighbour[a].push_back(e);

        e.to = a;
        e.weight = 0;
        e.toBackIndex = fromBackIndex;
        e.isOriginal=false;
        neighbour[b].push_back(e);

    }

    /**for(int i=0;i<n;i++){
        cout<<"Vertex: "<<i<<endl;
        for(auto it = neighbour[i].begin();it<neighbour[i].end();it++){
            Edge current = (*it);
            cout<<"   to: "<<current.to<<"  (weight="<<current.weight;
            cout<<";  backIndex="<<current.toBackIndex<<")";
            cout<<endl;
        }
    }*/

    //cin>>s>>t;
    s=0;
    t=n-1;
    while(true){
        pair<int,int> source[MAX_N];
        for(int i=0;i<n;i++){
            source[i].first=-1;
        }
        queue<int> q;
        q.push(s);
        while(!q.empty()){
            int currentVertex = q.front();
            q.pop();
            for(auto it = neighbour[currentVertex].begin();
                it<neighbour[currentVertex].end();it++){
                Edge currentEdge = (*it);
                if(currentEdge.weight>0&&source[currentEdge.to].first==-1){
                    source[currentEdge.to].first=currentVertex;
                    source[currentEdge.to].second=currentEdge.toBackIndex;
                    if(currentEdge.to==t){
                        while(!q.empty()){
                            q.pop();
                            break;
                        }
                    }else{
                        q.push(currentEdge.to);
                    }
                }
            }
        }

        if(source[t].first==-1){
            break;
        }


        /// add min

        int minVal=MAX_INT;

        for(int currentVertex = t; currentVertex!=s;
        currentVertex=source[currentVertex].first){

            int currentIndex = source[currentVertex].second;
            Edge e = neighbour[currentVertex][currentIndex];
            Edge prev = neighbour[e.to][e.toBackIndex];
            int currentW = prev.weight;
            if(minVal>currentW){
                minVal=currentW;
            }


        }
        if(minVal==0){
            break;
        }
        for(int currentVertex = t; currentVertex!=s;
        currentVertex=source[currentVertex].first){

            int currentIndex = source[currentVertex].second;
            Edge e = neighbour[currentVertex][currentIndex];
            //Edge prev = neighbour[e.to][e.toBackIndex];

            if(e.isOriginal){
                neighbour[currentVertex][currentIndex].weight-=minVal;
                neighbour[e.to][e.toBackIndex].weight+=minVal;
            }else{
                neighbour[currentVertex][currentIndex].weight+=minVal;
                neighbour[e.to][e.toBackIndex].weight-=minVal;
            }
        }
        /*cout<<"----------------------"<<endl;
        for(int i=0;i<n;i++){
            cout<<"for vertex "<<i<<endl;
            for(auto it = neighbour[i].begin();it<neighbour[i].end();it++){
                Edge current = (*it);
                if(current.isOriginal){
                cout<<"   ("<<current.to<<"; "<<current.weight<<")  ";
                }else{
                cout<<"   !("<<current.to<<"; "<<current.weight<<")  ";
                }
            }
            cout<<endl;
        }
        cout<<endl;
        cout<<"----------------------"<<endl;*/
    }

    int maxFlow=0;

    for(auto it = neighbour[t].begin();it<neighbour[t].end();it++){
        Edge e = (*it);
        maxFlow+=e.weight;
    }

    output<<maxFlow;

    input.close();
    output.close();

    return 0;

}