Pagini recente » Cod sursa (job #2895117) | Cod sursa (job #536075) | Cod sursa (job #467721) | Cod sursa (job #1153163) | Cod sursa (job #812667)
Cod sursa(job #812667)
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
inline int next_int() {
int d;
scanf("%d", &d);
return d;
}
const int INF = 1e9;
const int V = 351;
const int E = 12501;
int n, m, s, d;
int where, cost, flow, min_cost, max_flow, dist[V], prev[V];
vector<pair<int, int> > edges;
int capacity_e[V][V], cost_e[V][V];
void mcmf() {
while (true) {
for (int i = 1; i <= n; i++) {
dist[i] = INF;
prev[i] = -1;
}
dist[s] = 0;
for (int i = 1; i < n; i++) {
for (int j = edges.size() - 1; j >= 0; j--) {
int u = edges[j].first, v = edges[j].second;
if (dist[u] < INF && capacity_e[u][v] > 0 && dist[u] + cost_e[u][v] < dist[v]) {
dist[v] = dist[u] + cost_e[u][v];
prev[v] = u;
}
}
}
if (dist[d] == INF) {
return;
}
flow = INF, cost = 0, where = d;
while (prev[where] != -1) {
flow = min(flow, capacity_e[prev[where]][where]);
where = prev[where];
}
where = d;
while (prev[where] != -1) {
capacity_e[prev[where]][where] -= flow;
capacity_e[where][prev[where]] += flow;
cost += cost_e[prev[where]][where];
where = prev[where];
}
min_cost += flow * cost;
max_flow += flow;
}
}
int main() {
freopen("fmcm.in", "r", stdin);
freopen("fmcm.out", "w", stdout);
n = next_int();
m = next_int();
s = next_int();
d = next_int();
for (int i = 0; i < m; i++) {
int x = next_int();
int y = next_int();
int c = next_int();
int z = next_int();
capacity_e[x][y] = c, cost_e[x][y] = +z;
capacity_e[y][x] = 0, cost_e[y][x] = -z;
edges.push_back(make_pair(x, y));
edges.push_back(make_pair(y, x));
}
mcmf();
printf("%d\n", min_cost);
return 0;
}