Pagini recente » Cod sursa (job #844672) | Cod sursa (job #3260481) | Cod sursa (job #2340112) | Cod sursa (job #2161806) | Cod sursa (job #2124544)
#include <bits/stdc++.h>
#define mp make_pair
#define f first
#define s second
#define nmax 460
using namespace std;
ifstream fin("fmcm.in");
ofstream fout("fmcm.out");
typedef pair<int,int> per ;
int n,m,A,B,C,D,S,DE,i,ans,fc,j,cost_total;
int t[nmax];
int c[nmax][nmax],f[nmax][nmax],ct[nmax][nmax],d[nmax];
vector < int > v[nmax];
priority_queue < per, vector<per>, greater<per> > pq;
bool dijkstra()
{
int nod=0;
memset(d,1e6,sizeof(d));
memset(t,0,sizeof(t));
pq.push(mp(0,S));
t[S]=-1;
d[S]=0;
while(!pq.empty())
{
nod=pq.top().s;pq.pop();
for(auto it:v[nod])
{
if(t[it]||c[nod][it]==f[nod][it])continue;
if(d[nod]+ct[nod][it]<d[it])
{
d[it]=d[nod]+ct[nod][it];
t[it]=nod;
pq.push(mp(d[it],it));
}
}
}
if(t[DE])return true;
return false;
}
int main()
{
fin>>n>>m>>S>>DE;
for(i=1; i<=m; i++)
{
fin>>A>>B>>C>>D;
v[A].push_back(B);
v[B].push_back(A);
c[A][B]=C;
ct[A][B]=D;
ct[B][A]=-D;
}
while(dijkstra())
{
for(auto it:v[DE])
{
if(!t[it]||c[it][DE]==f[it][DE]||!c[it][DE])continue;
fc=c[it][DE]-f[it][DE];
cost_total=0;
for(j=DE; j!=S; j=t[j])
{
fc=min(fc,c[t[j]][j]-f[t[j]][j]);
}
if(!fc)continue;
for(j=DE; j!=S; j=t[j])
{
f[t[j]][j]+=fc;
f[j][t[j]]-=fc;
cost_total+=ct[t[j]][j];
}
ans+=cost_total*fc;
}
}
fout<<ans;
return 0;
}