Pagini recente » Cod sursa (job #1245000) | Cod sursa (job #163328) | Cod sursa (job #1628151) | Cod sursa (job #581225) | Cod sursa (job #2499253)
#include <iostream>
#include <vector>
#include <cstdio>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 350;
const int INF = (int) 1e9;
int n;
int m;
int s;
int d;
int cap[N][N];
int cost[N][N];
vector<int> g[N];
int best[N];
int par[N];
bool act[N];
int main()
{
freopen ("fmcm.in", "r", stdin);
cin >> n >> m >> s >> d;
s--;
d--;
for (int i = 1; i <= m; i++)
{
int a, b;
cin >> a >> b;
a--;
b--;
g[a].push_back(b);
b[g].push_back(a);
cin >> cap[a][b] >> cost[a][b];
cost[b][a] = -cost[a][b];
}
ll ans = 0;
while (1)
{
for (int i = 0; i < n; i++)
{
best[i] = INF;
}
act[s] = 1;
best[s] = 0;
queue<int> q;
q.push(s);
while (!q.empty())
{
int a = q.front();
q.pop();
act[a] = 0;
for (auto &b : g[a])
{
if (cap[a][b] > 0 && best[a] + cost[a][b] < best[b])
{
best[b] = best[a] + cost[a][b];
par[b] = a;
if (act[b] == 0)
{
act[b] = 1;
q.push(b);
}
}
}
}
if (best[d] == INF)
{
break;
}
int mn = INF, now = d;
while (now != s)
{
int a = par[now];
int b = now;
mn = min(mn, cap[a][b]);
now = par[now];
}
ans += (ll) best[d] * mn;
now = d;
while (now != s)
{
int a = par[now];
int b = now;
cap[a][b] -= mn;
cap[b][a] += mn;
now = par[now];
}
}
cout << ans << "\n";
}