Pagini recente » Cod sursa (job #664006) | Cod sursa (job #888857) | Cod sursa (job #2799226) | Cod sursa (job #1785742) | Cod sursa (job #2125056)
#include <fstream>
#define nmax 351
#define inf 2e9
#include <vector>
#include <queue>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
vector <int> v[nmax];
int C[nmax][nmax],F[nmax][nmax],cost[nmax][nmax],t[nmax],di[nmax],n,m,s,d,a,b,c,z,FC,ans;
priority_queue <pair<int,int>,vector <pair<int,int> >,greater <pair<int,int> > > h;
int dijkstra()
{
int i,nod,cst,vec;
for(i=1;i<=n;i++) {t[i]=0;di[i]=inf;}
h.push({0,s});
di[s]=0;
t[s]=-1;
while(!h.empty())
{
cst=h.top().first;
nod=h.top().second;
h.pop();
if(nod == d || cst!=di[nod])
continue;
for(i=0;i<v[nod].size();i++)
{
vec=v[nod][i];
if(di[vec]>cst+cost[nod][vec]&&C[nod][vec]>F[nod][vec]&&vec!=t[nod])
{
t[vec]=nod;
di[vec]=cst+cost[nod][vec];
h.push({cst+cost[nod][vec],vec});
}
}
}
if(t[d]) return 1;
return 0;
}
int main()
{
int i,vec,j;
f>>n>>m>>s>>d;
for(i=1;i<=m;i++)
{
f>>a>>b>>c>>z;
v[a].push_back(b);
v[b].push_back(a);
C[a][b]=c;
cost[a][b]=z;
cost[b][a]=-z;
}
while(dijkstra())
{
FC=inf;
for(j=d;j!=s;j=t[j]) FC=min(FC,C[t[j]][j]-F[t[j]][j]);
if(!FC) continue;
for(j=d;j!=s;j=t[j])
{
F[t[j]][j]+=FC;
F[j][t[j]]-=FC;
ans+=FC*cost[t[j]][j];
}
}
g<<ans;
return 0;
}