Pagini recente » Cod sursa (job #2396198) | Cod sursa (job #931687) | Cod sursa (job #127201) | Cod sursa (job #2380690) | Cod sursa (job #759503)
Cod sursa(job #759503)
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 1001
#define INF 0xffffff
vector<int>g[MAX];
int n,tt[MAX],cd[MAX],c[MAX][MAX],f[MAX][MAX]; //rezidual graph
bool viz[MAX];
bool bfs(){
int p,u,x,y;
cd[p=u=1]=1;
memset(viz,0,sizeof(viz));
viz[1]=1;
while(p<=u)
{
x=cd[p];
for(int i=0;i<g[x].size();i++)
{
y=g[x][i];
if(!viz[y] && c[x][y] - f[x][y] > 0)
{
viz[y]=1;
cd[++u]=y;
tt[y]=x;
if(y==n)return 1;
}
}
p++;
}
return 0;
}
int main(){
int m,x,y,z;
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d %d",&n,&m);
while(m--)
{
scanf("%d %d %d",&x,&y,&z);
g[x].push_back(y); //arcul direct
g[y].push_back(x); //arcul de intoarcere
c[x][y]=z; //capacitatea arcului
}
int mflux = 0,flux = INF;
for( ; bfs() ; flux = INF) //cit avem un drum de crestere
{
for(int x=n;x!=1;x=tt[x])flux=min(flux,c[tt[x]][x]-f[tt[x]][x]);
if(flux!=0)
{
for(int x=n;x!=1;x=tt[x])
{
f[tt[x]][x]+=flux;
f[x][tt[x]]-=flux;
}
mflux += flux;
}
/* for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)printf("%d ",f[i][j]); printf("\n");
}
printf("-----------------\n"); */
}
//bfs();
//for(int x=n;x!=1;x=tt[x])printf("%d -> ",x);
printf("%d\n",mflux);
return 0;
}