Pagini recente » Cod sursa (job #2093922) | Cod sursa (job #645505) | Cod sursa (job #2118266) | Cod sursa (job #2763738) | Cod sursa (job #1960271)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
const int nmax = 355;
const int inf = 2000000000;
int N,M,S,D,ans;
int dp[nmax],last[nmax];
int Cost[nmax][nmax],C[nmax][nmax],F[nmax][nmax];
bool viz[nmax];
vector < int > L[nmax];
queue < int > Q;
inline void Read()
{
int i,j,x,y,z,c;
fin >> N >> M >> S >> D;
for(i = 1; i <= M; i++)
{
fin >> x >> y >> z >> c;
Cost[x][y] = c; Cost[y][x] = -c;
C[x][y] = z;
L[x].push_back(y); L[y].push_back(x);
}
}
inline bool Bellman()
{
int i,nod;
for(i = 1; i <= N; i++)
{
dp[i] = inf;
last[i] = 0;
}
dp[S] = 0; viz[S] = true;
Q.push(S);
while(!Q.empty())
{
nod = Q.front();Q.pop();viz[nod] = false;
for(auto it : L[nod])
{
if(F[nod][it] < C[nod][it] && dp[it] > dp[nod] + Cost[nod][it])
{
dp[it] = dp[nod] + Cost[nod][it];
last[it] = nod;
if(!viz[it])
{
viz[it] = true;
Q.push(it);
}
}
}
}
return (dp[D] != inf);
}
inline void Solve()
{
int i,x,ccost,minflow,maxflow;
while(Bellman())
{
ccost = 0;
minflow = inf;
for(x = D; x != S; x = last[x])
{
minflow = min(minflow,C[last[x]][x] - F[last[x]][x]);
ccost += Cost[last[x]][x];
}
for(x = D; x != S; x = last[x])
{
F[last[x]][x] += minflow;
F[x][last[x]] -= minflow;
}
ans += (minflow*ccost);
}
fout << ans << "\n";
fout.close();
}
int main()
{
Read();
Solve();
return 0;
}