Pagini recente » Cod sursa (job #2987421) | Cod sursa (job #2849919) | Cod sursa (job #2340537) | Cod sursa (job #2053778) | Cod sursa (job #1761311)
#include <bits/stdc++.h>
using namespace std;
#define FILE_IO
int N, M, S, T, flow, cost;
int cp[405][405];
int cst[405][405];
int dst[405];
int inq[405];
int f[405];
queue <int> q;
vector <int> edg[405];
void BFS()
{
memset(f, 0, sizeof(f));
for(int i = 1; i <= N; i++)
dst[i] = 1 << 30;
dst[S] = 0;
inq[S] = 1;
f[S] = 0;
q.push(S);
while(!q.empty())
{
int x = q.front();
inq[x] = 0;
q.pop();
for(auto nxt: edg[x])
{
if(cp[x][nxt] == 0) continue;
int d = dst[x] + cst[x][nxt];
if(d < dst[nxt])
{
dst[nxt] = d;
f[nxt] = x;
if(!inq[nxt])
{
q.push(nxt);
inq[nxt] = 1;
}
}
}
}
}
int main()
{
#ifdef FILE_IO
freopen("fmcm.in", "r", stdin);
freopen("fmcm.out", "w", stdout);
#endif
scanf("%d%d%d%d", &N, &M, &S, &T);
for(int i = 1; i <= M; i++)
{
int x, y, cap, cost;
scanf("%d%d%d%d", &x, &y, &cap, &cost);
cp[x][y] = cap;
cp[y][x] = 0;
cst[x][y] = cost;
cst[y][x] = -cost;
edg[x].push_back(y);
edg[y].push_back(x);
}
flow = 0;
while(1)
{
BFS();
if(f[T] == 0) break;
int addFlow = 1 << 30;
int c = 0;
for(int nod = T; nod != S; nod = f[nod])
{
addFlow = min(addFlow, cp[ f[nod] ][nod]);
c += cst[ f[nod] ][nod];
}
flow += addFlow;
cost += addFlow * c;
for(int nod = T; nod != S; nod = f[nod])
{
cp[ f[nod] ][nod] -= addFlow;
cp[nod][ f[nod] ] += addFlow;
}
}
printf("%d\n", cost);
return 0;
}