Cod sursa(job #2457375)

Utilizator andrei42Oandrei42O andrei42O Data 17 septembrie 2019 16:40:07
Problema Flux maxim de cost minim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <bits/stdc++.h>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
const int N = 360;
const int oo = 1000000000;
int n, m, flux, Flux, s, d, x, y, c, z, fl[N][N], cap[N][N], cst[N], T[N];
vector <pair<int, int>> v[N];
bitset <N> Q;
queue <int> q;
int Bellman()
{
    T[s] = s;
    for(int i = 1; i <= n; i++)
        cst[i] = oo;
    cst[s] = 0;
    Q[s] = 1;
    q.push(s);
    while(!q.empty())
    {
        int nod = q.front();
        for(auto it:v[nod])
        {
            int vec, C;
            tie(vec, C) = it;
            if(cap[nod][vec] > fl[nod][vec])
            {
                if(cst[vec] > cst[nod] + C)
                {
                    cst[vec] = cst[nod] + C;
                    if(!Q[vec])
                    {
                        q.push(vec);
                        Q[vec] = 1;
                    }
                    T[vec] = nod;
                }
            }
        }
        q.pop();
        Q[nod] = 0;
    }
    if(cst[d] == oo)
        return 0;
    return 1;
}
void update()
{
    for(x=d,y = T[d], flux = oo; x != s; x = T[x], y = T[y])
        flux = min(flux, cap[y][x] - fl[y][x]);
    for(x=d,y = T[d]; x != s; x = T[x], y = T[y])
        fl[y][x] += flux, fl[x][y] -= flux;
    Flux += flux * cst[d];
}
int main()
{
    f >> n >> m >> s >> d;
    for(; m; m--)
    {
        f >> x >> y >> c >> z;
        v[x].push_back(make_pair(y, z));
        v[y].push_back(make_pair(x, -z));
        cap[x][y] = c;
    }
    while(Bellman())
        update();
    g << Flux;
    return 0;
}