Pagini recente » Cod sursa (job #1247022) | Cod sursa (job #2508070) | Cod sursa (job #997336) | Cod sursa (job #1847623) | Cod sursa (job #2209046)
#include <fstream>
#include <vector>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
const int N=1000;
const long long INF=550000000;
int n,m,q[N],s,d,pred[N],f[N][N],c[N][N];
bool viz[N],a[N][N];
void reset_viz()
{
for(int i=1;i<=n;i++)
viz[i]=false;
}
bool bfs()
{
int st=0,dr=-1,x;
reset_viz();
q[++dr]=s;
while(st<=dr)
{
x=q[st++];
for(int y=1;y<=n;y++)
{
if(!viz[y] && c[x][y]>f[x][y])
{
q[++dr]=y;
viz[y]=true;
pred[y]=x;
if(y==d)
return true;
}
}
}
return false;
}
int min_drum()
{
int x=d,vmin=INF;
while(x!=s)
{
vmin=min(vmin,c[pred[x]][x]-f[pred[x]][x]);
x=pred[x];
}
return vmin;
}
void actualizare_drum(int vmin)
{
int x=d;
while(x!=s)
{
f[pred[x]][x]+=vmin;
f[x][pred[x]]-=vmin;
x = pred[x];
}
}
int main()
{
int x,y,cap,vmin,flux=0;
in>>n>>m;
s=1;
d=n;
for(int i=1;i<=m;i++)
{
in>>x>>y>>cap;
a[x][y]=true;
c[x][y]=cap;
}
while(bfs())
{
vmin=min_drum();
flux+=vmin;
actualizare_drum(vmin);
}
out<<flux;
return 0;
}