Cod sursa(job #1892462)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 24 februarie 2017 23:19:30
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include <fstream>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

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

int cap[1005][1005], flx[1005][1005], x, y, i, n, m, c, l;
bool viz[1005];
int wh[1005], sol;
vector<int>ls[1005];
bool ok;

bool df() {
    queue<int> q;
    memset(viz, 0,sizeof(viz));
    q.push(1);
    viz[1]=1;
    while (q.empty() == 0) {
        x = q.front();
        q.pop();
        if (x==n)
            continue;
        l = ls[x].size();
        for (i = 0; i < l; i++) {
            y = ls[x][i];
            if (cap[x][y] - flx[x][y] > 0 && viz[y] ==0) {
                viz[y] = 1;
                wh[y] = x;
                q.push(y);
            }
        }
    }
    return viz[n];
}

int main() {
    f >> n >> m;
    while (m--) {
        f >> x >> y >> c;
        ls[x].push_back(y);
        ls[y].push_back(x);
        cap[x][y] += c;
        //cap[y][x] = -c;
    }
    ok = 1;
    while (df()) {
        for (i = 0; i < ls[n].size(); i++) {
            y = ls[n][i];
            if (cap[y][n]-flx[y][x]== 0)
                continue;
            wh[n]=y;
            //if ()
            int minim = 100000005;
            y = n;
            while (y>1) {
                if (cap[wh[y]][y]-flx[wh[y]][y] <= 0)
                    continue;
                minim = min(minim,cap[wh[y]][y]-flx[wh[y]][y]);
                y = wh[y];
            }
            sol += minim;
            y = n;
            while (y>1) {
                flx[wh[y]][y] += minim;
                flx[y][wh[y]] -= minim;
                y = wh[y];
            }
        }
    }
    g << sol;
}