Cod sursa(job #2029100)

Utilizator vladttturcuman vlad vladtt Data 29 septembrie 2017 11:57:06
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 2.23 kb
//
//  L.cpp
//
//  Created by Vlad Turcuman on 24/09/2017.
//  Copyright © 2017 Vlad Turcuman. All rights reserved.
//

#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <map>

#define pii pair<int,int>
#define fs first
#define sc second
#define NMax 1010
#define oo 2000000000

using namespace std;

struct queElement{
    int a,b;
    int flow,ant;
}tmp,tmp2;

int No,n,maxFlow;

vector<int> edges[NMax];
vector<queElement> Que;
int vis[NMax];
pii mat[NMax][NMax];


int bfs()
{
    No ++;
    
    vis[1] = No;
    Que.clear();

    tmp.a = -1;
    tmp.b = 1;
    tmp.ant = -1;
    tmp.flow = oo;
    
    Que.push_back(tmp);
    
    int iterator = 0;
    
    while(iterator<Que.size())
    {
        tmp = Que[iterator++];
        
        for(int i=0;i<edges[tmp.b].size();i++)
        {
            if(vis[edges[tmp.b][i]] != No)
            {
                
                tmp2.a = tmp.b;
                tmp2.b = edges[tmp.b][i];
                tmp2.ant = iterator-1;
                tmp2.flow = min(tmp.flow, mat[tmp2.a][tmp2.b].sc - mat[tmp2.a][tmp2.b].fs);
                
                if(tmp2.flow > 0)
                {
                    vis[edges[tmp.b][i]] = No;
                    Que.push_back(tmp2);
                    if(tmp2.b == n)
                        return tmp2.flow;
                }
            }
        }
    }
    return 0;
    
}

void FindMaxFlow()
{
    maxFlow = 0;
    
    int flow;
    
    while(flow = bfs())
    {
        
        /// Find the path
        int iterator = Que.size()-1;
        
        while(iterator > 0)
        {
            int antNode = Que[iterator].a;
            int curNode = Que[iterator].b;
            
            mat[antNode][curNode].fs += flow;
            mat[curNode][antNode].fs -= flow;
                  
            iterator = Que[iterator].ant;
        }
        
        maxFlow += flow;
        
    }
    
}

int main()
{
    int m,a,b,c;
    cin>>n>>m;
    
    for(int i=1;i<=m;i++)
    {
        cin>>a>>b>>c;
        
        mat[a][b].sc = c;
        
        edges[a].push_back(b);
        edges[b].push_back(a);
    }
    
    FindMaxFlow();
    
    cout<<maxFlow;
    return 0;
}