Pagini recente » Cod sursa (job #2028567) | Cod sursa (job #2829933) | Cod sursa (job #2177056) | Cod sursa (job #888249) | Cod sursa (job #2415460)
#include <bits/stdc++.h>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
int n,m,s,d,x,y,z,c,ans,tata[360],dst[360],dst_real[360],dst_actl[360];
int cost[360][360],cap[360][360];
vector<int> v[360];
void bellman(){
queue<int> q;
memset(dst,127,sizeof(dst));
dst[s]=0;q.push(s);
while(q.size()){
int poz=q.front();q.pop();
for(auto it:v[poz])
if(cap[poz][it])
if(dst[it]>dst[poz]+cost[poz][it]){
dst[it]=dst[poz]+cost[poz][it];
q.push(it);
}
}
}
bool djikstra(){
priority_queue<pair<int,int> > q;
memset(dst_real, 0,sizeof(dst_real));
memset(dst_actl,127,sizeof(dst_actl));
tata[s]=tata[d]=0;
dst_actl[s]=0;q.push({0,s});
while(q.size()){
int dist=-q.top().first;
int poz = q.top().second;q.pop();
if(dst_actl[poz]!=dist)continue;
for(auto it:v[poz])
if(cap[poz][it])
if(dst_actl[it]>dst_actl[poz]+cost[poz][it]+dst[poz]-dst[it]){
dst_actl[it]=dst_actl[poz]+cost[poz][it]+dst[poz]-dst[it];
dst_real[it]=dst_real[poz]+cost[poz][it];
tata[it]=poz;
q.push({-dst_actl[it],it});
}
}
for(int i=1;i<=n;i++)
dst[i]=dst_real[i];
return (tata[d]!=0);
}
int main()
{
f>>n>>m>>s>>d;
for(;m;m--){
f>>x>>y>>z>>c;
v[x].push_back(y);
v[y].push_back(x);
cost[x][y]= c;
cost[y][x]=-c;
cap[x][y]=z;
}
bellman();
for(;djikstra();){
int flxmax=1e9,cst=0;
for(int poz=d;poz!=s;poz=tata[poz]){
cst+=cost[tata[poz]][poz];
flxmax=min(flxmax,cap[tata[poz]][poz]);
}
for(int poz=d;poz!=s;poz=tata[poz]){
cap[tata[poz]][poz]-=flxmax;
cap[poz][tata[poz]]+=flxmax;
}
ans+=flxmax*cst;
}
g<<ans;
return 0;
}