Cod sursa(job #1968520)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 17 aprilie 2017 18:52:25
Problema Flux maxim de cost minim Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.54 kb
#include <bits/stdc++.h>

using namespace std;

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

const int nmax = 355, f_mare =  2000000005;
vector<int>ls[nmax];
int cs[nmax][nmax], cap[nmax][nmax], fl[nmax][nmax], X, Y, x, y, l,sol,c,z,fm, n, m, i, j;
int dist[nmax], tt[nmax];
bool viz[nmax],ok;

int bfs() {
    queue<int>q;
    q.push(X);
    for (i=1;i<=n;i++)dist[i]=f_mare,tt[i]=-1,viz[i]=0;
    dist[X] = 0;
    viz[X] = 1;
    while (q.empty() == 0) {
        x = q.front();
        q.pop();
        l = ls[x].size();
        for (i = 0; i < l; i++) {
            y = ls[x][i];
            c = cs[x][y];
            if (dist[x] + c < dist[y] && cap[x][y] - fl[x][y] > 0) {
                dist[y] = dist[x]+c;
                tt[y] = x;
                if (viz[y]==0){
                  viz[y]=1;q.push(y);
                }
            }
        }
    }
    if (dist[Y] == f_mare) return 0;
    ok=1;
    y = Y;
    fm = f_mare;
    while (y != X) {
        fm = min(fm, cap[tt[y]][y] - fl[tt[y]][y]);
        y = tt[y];
    }
    y = Y;
    while (y != X) {
        fl[tt[y]][y] += fm, fl[y][tt[y]] -= fm;
        y = tt[y];
    }
    return dist[Y]*fm;
}

int main() {
    f >> n >> m >> X >> Y;
    for (i = 1; i <= m; i++) {
        f >> x >> y >> c >> z;
        ls[x].push_back(y);
        ls[y].push_back(x);
        cap[x][y] = c;
        cs[x][y] = z;
        cs[y][x] = -z;
    }
    ok=1;
    while (ok) {ok=0;
        x = bfs();
        sol += x;
    }
    g << sol;

    return 0;
}