Pagini recente » Cod sursa (job #1444283) | Cod sursa (job #1185090) | Cod sursa (job #1277283) | Cod sursa (job #1362236) | Cod sursa (job #963258)
Cod sursa(job #963258)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
#define inf 1<<30
#define LE 1066
int que[LE+666],t,k,father[LE];
int fl[LE][LE],mxl[LE],n,m;
bool viz[LE];
int i,xx,yy,cap,flux;
int fmax,node;
void bfs()
{
for(int i=1;i<=n;++i) viz[i]=false;
que[(k=1)]=1,viz[1]=true;
t=0;
for(int i=1;i<=k;++i){
for(int j=1;j<n;++j)
if (viz[j]==false&&fl[que[i]][j]>0){
viz[j]=true;
que[++k]=j;
father[j]=que[i];
}
if (fl[que[i]][n]>0) mxl[++t]=que[i];
}
}
int main()
{
f>>n>>m;
for(i=1;i<=m;++i){
f>>xx>>yy>>cap;
fl[xx][yy]=cap;
}
while (true)
{
bfs();
if (t==0) break;
for(i=1;i<=t;++i){
fmax=inf;
father[n]=mxl[i],node=n;
while (node!=1){
fmax=min(fmax,fl[father[node]][node]);
node=father[node];
}
if(fmax==0) continue;
node=n;
while (node!=1){
fl[father[node]][node]-=fmax;
fl[node][father[node]]+=fmax;
node=father[node];
}
flux+=fmax;
}
}
g<<flux<<'\n';
f.close();
g.close();
return 0;
}