Pagini recente » Borderou de evaluare (job #595840) | Borderou de evaluare (job #2022159) | Borderou de evaluare (job #1039880) | Borderou de evaluare (job #16615) | Cod sursa (job #1967457)
#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();
}