Pagini recente » Cod sursa (job #1691039) | Cod sursa (job #2554926) | Cod sursa (job #2152299) | Borderou de evaluare (job #2100066) | Cod sursa (job #461578)
Cod sursa(job #461578)
#include <stdio.h>
#define Nmax 351
#define Inf 1000000000
int cost[Nmax][Nmax];
int flux[Nmax][Nmax];
int cap[Nmax][Nmax];
int n,m, Sursa, Destinatie;
int l[Nmax][Nmax];
int dist_min[Nmax];
int q[Nmax*Nmax];
int t[Nmax];
char viz[Nmax];
int rasp = 0;
int belman() {
int i, st = 0, dr = 1;
for (i = 1; i <= n; ++i) {
dist_min[i] = Inf;
viz[i] = 0;
}
q[1] = Sursa;
dist_min[Sursa] = 0;
viz[Sursa] = 1;
while (st < dr) {
int nod = q[++st];
for (i = 1; i <= l[nod][0]; ++i)
if ((cap[nod][l[nod][i]] - flux[nod][l[nod][i]] > 0) &&
(dist_min[l[nod][i]] > dist_min[nod] + cost[nod][l[nod][i]])) {
dist_min[l[nod][i]] = dist_min[nod] + cost[nod][l[nod][i]];
t[l[nod][i]] = nod;
if (viz[l[nod][i]] == 0) {
viz[l[nod][i]] = 1;
q[++dr] = l[nod][i];
}
}
viz[nod] = 0;
}
return dist_min[Destinatie] < Inf;
}
void baga_fluxu() {
int Min = cap[t[Destinatie]][Destinatie] - flux[t[Destinatie]][Destinatie];
int nod;
for (nod = t[Destinatie]; nod != Sursa; nod = t[nod])
if (Min > cap[t[nod]][nod] - flux[t[nod]][nod])
Min = cap[t[nod]][nod] - flux[t[nod]][nod];
for (nod = Destinatie; nod != Sursa; nod = t[nod]) {
flux[t[nod]][nod] += Min;
flux[nod][t[nod]] -= Min;
}
rasp += Min*dist_min[Destinatie];
}
int main() {
freopen("fmcm.in" , "r", stdin );
freopen("fmcm.out", "w", stdout);
scanf("%d%d%d%d", &n, &m, &Sursa, &Destinatie);
int i, A, B;
for (i = 1; i <= m; ++i) {
scanf("%d%d", &A, &B);
scanf("%d%d", &cap[A][B], &cost[A][B]);
cost[B][A] = - cost[A][B];
l[A][++l[A][0]] = B;
l[B][++l[B][0]] = A;
}
while (belman())
baga_fluxu();
printf("%d\n", rasp);
return 0;
}