Pagini recente » Cod sursa (job #1953750) | Cod sursa (job #2283997) | Cod sursa (job #1737007) | Cod sursa (job #1372613) | Cod sursa (job #979524)
Cod sursa(job #979524)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("fmcm.in");
ofstream g("fmcm.out");
#define x first
#define y second
#define mp make_pair
#define pii pair<int,int>
#define inf (1<<30)
#define LE 366
#define R return
#include <iostream>
int nod;
int dp[LE],father[LE],que[LE*66];
pii cap[LE][LE];
int S,D,k,i,j,n,m;
int result,fres;
bool Flux() {
que[(k=1)]=S;
for(i=1; i<=n; ++i) dp[i]=inf,father[i]=0;
dp[S]=-inf;
for(i=1; i<=k; ++i) {
for(j=1; j<=n; ++j)
if (cap[que[i]][j].x>0)
if (dp[j]>dp[que[i]]+cap[que[i]][j].y) {
dp[j]=dp[que[i]]+cap[que[i]][j].y;
father[j]=que[i];
que[++k]=j;
}
}
if (dp[D]==inf) R false;
R true;
}
int main() {
f>>n>>m>>S>>D;
for(i=1; i<=m; ++i) {
int xx,yy,cp,cc;
f>>xx>>yy>>cp>>cc;
cap[xx][yy]=mp(cp,cc);
cap[yy][xx]=mp(0,-cc);
}
while (Flux()){
int fmax=inf;
nod=D;
//cout<<-1<<'\n';
//for(i=1;i<=n;++i) cout<<father[i]<<" ";
//cout<<'\n';
while (father[nod]!=0){
fmax=min(fmax,cap[father[nod]][nod].x);
nod=father[nod];
// cout<<nod<<" ";
}
//cout<<-1;
nod=D;
while (father[nod]!=0){
cap[father[nod]][nod].x-=fmax;
cap[nod][father[nod]].x+=fmax;
result+=cap[father[nod]][nod].y*fmax;
nod=father[nod];
}
fres+=fmax;
}
g<<result<<'\n';
f.close();
g.close();
return 0;
}