Pagini recente » Cod sursa (job #1009028) | Cod sursa (job #1628504) | Cod sursa (job #2820838) | Cod sursa (job #2496675) | Cod sursa (job #1871291)
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#define pii pair <int, int>
#define f first
#define s second
using namespace std;
int n, m, S, D;
vector <int> v[400];
int c[400][400], f[400][400], cost[400][400], t[400], drum[400];
bool inq[400];
queue <int> q;
inline bool bellman ()
{
t[S] = -1;
drum[S] = 0;
q.push (S);
inq[S] = true;
bool OK = false;
for (; !q.empty ();)
{
int nod = q.front ();
q.pop ();
for (auto &it : v[nod])
if (f[nod][it] < c[nod][it] && drum[nod] + cost[nod][it] < drum[it])
{
if (it == D)
OK = true;
drum[it] = drum[nod] + cost[nod][it];
t[it] = nod;
if (!inq[it])
q.push (it),
inq[it] = true;
}
inq[nod] = 0;
}
return OK;
}
int main ()
{
freopen ("fmcm.in", "r", stdin);
freopen ("fmcm.out", "w", stdout);
scanf ("%d %d %d %d", &n, &m, &S, &D);
for (int i = 1; i <= m; ++i)
{
int x, y, costt, cap;
scanf ("%d %d %d %d", &x, &y, &cap, &costt);
v[x].push_back (y);
v[y].push_back (x);
c[x][y] = cap;
cost[x][y] = costt;
cost[y][x] = -costt;
}
for (int i = 1; i <= n; ++i)
drum[i] = 2000000000;
long long cos = 0LL;
for (; bellman ();)
{
int nod = D, mi = 2000000000;
for (; t[nod] != -1; nod = t[nod])
mi = min (mi, c[t[nod]][nod] - f[t[nod]][nod]);
nod = D;
for (; t[nod] != -1; nod = t[nod])
f[t[nod]][nod] += mi,
f[nod][t[nod]] -= mi,
cos += 1LL * mi * cost[t[nod]][nod];
for (int i = 1; i <= n; ++i)
t[i] = 0, drum[i] = 2000000000, inq[i] = false;
}
printf ("%lld\n", cos);
return 0;
}