Cod sursa(job #3188158)

Utilizator Chris_BlackBlaga Cristian Chris_Black Data 1 ianuarie 2024 23:36:48
Problema Flux maxim de cost minim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.93 kb
#include <bits/stdc++.h>

using namespace std;


#define all(x) (x).begin(), (x).end()
#define allg(x) (x).begin(), (x).end(), greater<int>()
using ull = unsigned long long;
using ll =  long long;
using ld = double;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<ld>;
using si = set<int>;
using ssi = set<si>;
using sl = set<ll>;
using ssl = set<sl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vs = vector<string>;
using vpi = vector<pii>;
using vpl = vector<pll>;
using vvpl = vector<vpl>;
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define FORR(i, a, b) for(int i = a; i >= b; --i)
#define umap unordered_map
#define eb emplace_back
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define ff first
#define ss second
#define ar array

  inline void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}


const string TASK("fmcm");
ifstream fin(TASK + ".in");
ofstream fout(TASK + ".out");
#define cin fin
#define cout fout

const int Inf = 0x3f3f3f3f;
const ll N = 359;

int n, m, S, D;
int C[N][N], t[N];
int dbell[N], d[N];
vvpl G(N), GI(N);
int MCMF;

inline void BellMan()
{
    bitset<N> inq;
    queue<int> q;
    q.push(S);
    inq[S] = true;

    FOR(i, 1, n)dbell[i] = Inf;
    dbell[S] = 0;

    while(!q.empty())
    {
        int x = q.front();
        inq[x] = false;
        q.pop();

        for(auto [y, z] : G[x])
            if(C[x][y] && dbell[y] > dbell[x] + z)
            {
                dbell[y] = dbell[x] + z;
                if(!inq[y])q.push(y);
            }
    }
}

priority_queue<pii, vector<pii>, greater<pii>> q;
inline bool Dijkstra()
{
    q.push({0, S});

    FOR(i, 1, n)d[i] = Inf;
    d[S] = 0;
    t[S] = 0;

    while(!q.empty())
    {
        int dx = q.top().ff, x = q.top().ss;
        q.pop();

        if(dx > d[x])continue;

        for(auto [y, z] : G[x])
            if(C[x][y] && d[y] > d[x] + z + dbell[x] - dbell[y])
            {
                t[y] = x;
                d[y] = d[x] + z + dbell[x] - dbell[y];
                q.push({d[y], y});
            }
    }

    if(d[D] == Inf)return false;


    int min_flow = Inf;
    ll cost = dbell[D] + d[D];

    for(int x = D; x != S; x = t[x])
        min_flow = min(min_flow, C[t[x]][x]);

    for(int x = D; x != S; x = t[x])
    {
        C[t[x]][x] -= min_flow;
        C[x][t[x]] += min_flow;
    }

    MCMF += min_flow * cost;

    return true;
}


int main() {
    fastio();

    cin >> n >> m >> S >> D;

    int x, y, c, z;
    FOR(i, 1, m)
    {
        cin >> x >> y >> c >> z;
        C[x][y] += c;
        G[x].pb({y, z});
        G[y].pb({x, -z});
    }

    BellMan();
    for(; Dijkstra(););
    cout << MCMF << '\n';

    return 0;
}