Pagini recente » Cod sursa (job #2095061) | Cod sursa (job #2805829) | Cod sursa (job #1987368) | Cod sursa (job #2409206) | Cod sursa (job #1827271)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 351;
const int INF = 2e9;
vector <int> g[MAXN];
queue <int> q;
int flow[MAXN][MAXN], cap[MAXN][MAXN], cost[MAXN][MAXN];
int father[MAXN], inq[MAXN], dist[MAXN];
int bellman(int s, int d) {
int node;
for (int i = 0; i < MAXN; ++i)
dist[i] = INF;
inq[s] = 1;
dist[s] = 0;
q.push(s);
while (q.empty() == 0) {
node = q.front();
inq[node] = 0;
q.pop();
for (auto it : g[node])
if (flow[node][it] < cap[node][it] && dist[it] > dist[node] + cost[node][it]) {
dist[it] = dist[node] + cost[node][it];
father[it] = node;
if (inq[it] == 0) {
inq[it] = 1;
q.push(it);
}
}
}
return (dist[d] < INF);
}
inline int minimum(int A, int B) {
if (A < B)
return A;
return B;
}
int main()
{
int n, m, s, d, x, y, c, z, node, fl, ans;
ifstream fin("fmcm.in");
fin >> n >> m >> s >> d;
for (int i = 0; i < m; ++i) {
fin >> x >> y >> c >> z;
g[x].push_back(y);
g[y].push_back(x);
cap[x][y] = c;
cost[x][y] = z;
cost[y][x] = -z;
}
fin.close();
ans = 0;
while (bellman(s, d)) {
for (fl = INF, node = d; node != s; node = father[node])
fl = minimum(fl, cap[father[node]][node] - flow[father[node]][node]);
for (node = d; node != s; node = father[node]) {
flow[father[node]][node] += fl;
flow[node][father[node]] -= fl;
}
ans += fl * dist[d];
}
ofstream fout("fmcm.out");
fout << ans << '\n';
fout.close();
return 0;
}