Cod sursa(job #2843094)

Utilizator roberttrutaTruta Robert roberttruta Data 1 februarie 2022 22:58:22
Problema Flux maxim Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.16 kb
#include <fstream>
#include <vector>

using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");

#define inf 999999
vector <pair<int, int>> graf[1005];
int n;
void del_ege(int nod, int poz)
{
    int len = graf[nod].size() - 1;
    graf[nod][poz].first = graf[nod][len].first;
    graf[nod][poz].second = graf[nod][len].second;
    graf[nod].pop_back();
}
void dfs(int start, int parent, int* flow)
{
    if(graf[start].size() == 0)
    {
        if(start != n)
        {
            *flow = 0;
            del_ege(parent, 0);
        }
    }
    else
    {
        *flow = min(*flow, graf[start][0].second);
        dfs(graf[start][0].first, start, flow);

        graf[start][0].second -= *flow;
        if(graf[start][0].second == 0)
        {
            del_ege(start, 0);
        }
    }
}
int main()
{
    int m,i,a,b,c;
    f>>n>>m;
    for(i=1; i<=m; i++)
    {
        f>>a>>b>>c;
        graf[a].push_back(make_pair(b, c));
    }
    int flow, maxflow=0;
    while(graf[1].size() != 0)
    {
        flow = inf;
        dfs(1, 0, &flow);
        maxflow += flow;
    }
    g<<maxflow;
    return 0;
}