Cod sursa(job #3302738)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 10 iulie 2025 13:49:42
Problema Flux maxim de cost minim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.33 kb
#include <fstream>
#include <vector>
#include <queue>
#include <cassert>

#define ll long long

using namespace std;

ifstream cin("fmcm.in");
ofstream cout("fmcm.out");

const int NMAX = 350;
const int INF = 2e9;
const int MMAX = 12500 * 2;

struct PQElement {
    int node;
    int cost;
    bool operator < (const PQElement &other) const {
        return other.cost < cost;
    }
};

struct Edge {
    int a, b, capacity, cost;
}edges[MMAX + 1];

int n, m, source, dest;
vector<pair<int, int>> g[NMAX + 1];
int bf_cost[NMAX + 1];
bool in_q[NMAX + 1];
int cost[NMAX + 1];
pair<int, int> parent[NMAX + 1];

void add_edge(int a, int b, int c, int cost, int i) {
    g[a].push_back({b, i});
    g[b].push_back({a, i + m});
    edges[i] = {a, b, c, cost};
    edges[i + m] = {a, b, 0, -cost};
}

bool dijkstra() {
    for(int i = 1; i <= n; i++) {
        cost[i] = INF;
        parent[i] = {0, 0};
    }
    cost[source] = 0;

    priority_queue<PQElement> pq;
    pq.push({source, 0});
    while(!pq.empty()) {
        auto [node, curent_cost] = pq.top();
        pq.pop();

        if(cost[node] != curent_cost) {
            continue;
        }

        for(auto [next_node, edge_id] : g[node]) {
            int next_cost = curent_cost + edges[edge_id].cost + bf_cost[node] - bf_cost[next_node];
            if(edges[edge_id].capacity > 0 && next_cost < cost[next_node]) {
                cost[next_node] = next_cost;
                parent[next_node] = {node, edge_id};
                pq.push({next_node, next_cost});
            }
        }
    }
    return parent[dest].first != 0;
}

void bellman_ford() {
    queue<int> q;
    for(int i = 1; i <= n; i++) {
        in_q[i] = 1;
        q.push(i);
    }
    while(!q.empty()) {
        int node = q.front();
        q.pop();
        in_q[node] = 0;

        for(auto [next_node, edge_id] : g[node]) {
            int next_cost = bf_cost[node] + edges[edge_id].cost;
            if(edges[edge_id].capacity > 0 && next_cost < bf_cost[next_node]) {
                bf_cost[next_node] = next_cost;
                if(!in_q[next_node]) {
                    in_q[next_node] = 1;
                    q.push(next_node);
                }
            }
        }
    }
}

void min_cost_max_flow() {
    int min_cost = 0;
    bellman_ford();
    while(dijkstra()) {
        for(int i = 1; i <= n; i++) {
            if(cost[i] < INF) {
                bf_cost[i] += cost[i];
            }
        }
        
        int min_flow = INF;
        for(int node = dest; node != source; node = parent[node].first) {
            min_flow = min(min_flow, edges[parent[node].second].capacity);
        }

        min_cost += min_flow * bf_cost[dest];
        for(int node = dest; node != source; node = parent[node].first) {
            edges[parent[node].second].capacity -= min_flow;
            edges[parent[node].second + (parent[node].second <= m ? m : -m)].capacity += min_flow;
        }
    }
    cout << min_cost << '\n';
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> n >> m >> source >> dest;
    for(int i = 1; i <= m; i++) {
        int a, b, c, cost;
        cin >> a >> b >> c >> cost;
        add_edge(a, b, c, cost, i);
    }

    min_cost_max_flow();

    return 0;
}