Cod sursa(job #2301058)

Utilizator YouDontNeedMyNameJurcut Paul YouDontNeedMyName Data 12 decembrie 2018 16:36:56
Problema Flux maxim Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.61 kb
#include <bits/stdc++.h>
#define N 1005
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
int n,m,pater[N],graph[N][N],Rgraph[N][N],ap[N];
deque <int> q;
vector <int> lista[N];
bool BFS(int node)
{
    memset(pater,-1,sizeof(pater));
    pater[node]=0;
    q.push_back(node);
    while(!q.empty())
    {
        int p=q.front();
        q.pop_front();
        for(int i=0; i<lista[p].size(); i++)
        {
            if(graph[p][lista[p][i]]>0 && pater[lista[p][i]]==-1)
            {
                pater[lista[p][i]]=p;
                q.push_back(lista[p][i]);
            }
        }
    }
    if(pater[n]!=-1)
        return true;
    return false;
}
int main()
{
    in >> n >> m;
    for(int i=1; i<=m; i++)
    {
        int x,y;
        in >> x >> y;
        in >> graph[x][y];
        lista[x].push_back(y);
        lista[y].push_back(x);
    }
    int max_flow=0;
    while(BFS(1))
    {
        for(auto node:lista[n])
        {
            if(pater[node]==-1) continue;
            int current_flow=99999999;
            for(int i=node; i!=1; i=pater[i])
            {
                int j=pater[i];
                current_flow=min(current_flow,graph[j][i]);
            }
            graph[node][n]-=current_flow;
            graph[n][node]+=current_flow;
            for(int i=node; i!=1; i=pater[i])
            {
                int j=pater[i];
                graph[j][i]-=current_flow;
                graph[i][j]+=current_flow;
            }
            max_flow+=current_flow;
        }
    }
    out << max_flow;
    return 0;
}