Cod sursa(job #2119896)

Utilizator omegasFilip Ion omegas Data 1 februarie 2018 18:54:36
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.43 kb
#include <iostream>
#include <fstream>

using namespace std;

ifstream fin("maxflow.in");
ofstream fout("maxflow.out");

struct graf{
int nod;
graf* next;
};

int n;

graf* g[1001];
int res[1001][1001];

int flow;
int c[1001];
int q[1001];
int a,b;
int tati[1001];

void add(int a, int b, graf* v[])
{
    graf* q = new graf;
    q->nod  = b;
    q->next = v[a];
    v[a] = q;
}

void BFS(int k){
    graf*  p;
    for(p = g[k]; p!=NULL; p=p->next){
        if(c[p->nod] == 0 && res[k][p->nod] != 0)
            ++b,
            q[b] = p->nod,
            c[p->nod] = 1,
            tati[p->nod] = k;
        if(p->nod == n)
            return;
    }
    ++a;

    if(b >= a)
        BFS(q[a]);
}

void findPath(){

}

void update(){
    int i;
    for(i=1;i<=n;++i)
        c[i] = 0;
    a = b = 0;
    BFS(1);

    int x = n;
    int minim = 100200;

    while(x != 1){
        if(minim > res[tati[x]][x])
            minim = res[tati[x]][x];
        x = tati[x];
    }

    x = n;
    while(x != 1){
        res[tati[x]][x] = res[tati[x]][x] - minim;
        flow = flow + minim;
        x = tati[x];
    }
}

int main()
{
    int m,x,y,z,i,j;
    fin >> n >> m;
    for(i=1;i<=m;++i){
        fin >> x >> y >> z;
        add(x,y,g);
        res[x][y] = z;
    }

    while(true){
    update();
    if(c[n] == 0)
        break;
    }
    fout << flow;

    return 0;
}