Pagini recente » Cod sursa (job #86117) | Cod sursa (job #3208377) | Cod sursa (job #440157) | Cod sursa (job #1811950) | Cod sursa (job #2785490)
#include <bits/stdc++.h>
using namespace std;
ifstream in("fmcm.in");
ofstream out("fmcm.out");
const int inf=2e9+7;
vector<int> vec[705];
int cap[705][705];
int cost[705][705];
bool inq[705];
int dist[705];
int prv[705];
queue<int> q;
int n,m,st,dr;
int x,y,z,t;
int main()
{
in>>n>>m>>st>>dr;
for(int i=1;i<=m;++i)
{
in>>x>>y>>z>>t;
vec[x].push_back(y);
vec[y].push_back(x);
cap[x][y]=z;
cap[y][x]=0;
cost[x][y]=t;
cost[y][x]=-t;
}
int flow=0,price=0;
do
{
for(int i=1;i<=n;++i)
dist[i]=inf,prv[i]=-1,inq[i]=false;
dist[st]=0;
prv[st]=0;
q.push(st);
inq[st]=true;
while(!q.empty())
{
int x=q.front(); q.pop();
inq[x]=false;
for(int y:vec[x])
if(dist[y]>dist[x]+cost[x][y] and cap[x][y]>0)
{
dist[y]=dist[x]+cost[x][y];
prv[y]=x;
if(!inq[y])
inq[y]=true,
q.push(y);
}
}
if(prv[dr]!=-1)
{
int el=dr;
int minim=inf;
int addons=0;
while(prv[el]>0)
minim=min(minim,cap[prv[el]][el]),
addons+=cost[prv[el]][el],
el=prv[el];
flow+=minim;
price+=minim*addons;
el=dr;
while(prv[el]>0)
cap[prv[el]][el]-=minim,
cap[el][prv[el]]+=minim,
el=prv[el];
}
}while(prv[dr]!=-1);
//out<<flow<<' '<<price<<'\n';
out<<price<<'\n';
return 0;
}