Pagini recente » Cod sursa (job #2389953) | Cod sursa (job #838075) | Cod sursa (job #1724161) | Cod sursa (job #100858) | Cod sursa (job #767542)
Cod sursa(job #767542)
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 1001
#define INF 0xfffffff
vector<int>g[MAX];
int n,c[MAX][MAX],f[MAX][MAX],cd[MAX*MAX],tt[MAX];
bool was[MAX];
bool dfs(){
int p ,u ,x ,y;
cd[p = u = 1] = 1;
memset(was,0,sizeof(was));
was[1] = 1;
while(p <= u)
{
x = cd[p++];
for(int i=0;i<g[x].size();i++)
{
y = g[x][i];
if( !was[y] && c[x][y] > f[x][y] )
{
was[y] = 1;
if(y != n) cd[++u] = y;
tt[y] = x;
}
}
}
return was[n];
}
void maxflow(){
int max_flux = 0, flux;
int step = 0;
while( dfs() )
{
for(int i=0;i<g[n].size();i++)
{
tt[n] = g[n][i];
flux = INF;
for(int x = n;x!=1;x = tt[x]) flux = min(flux, c[tt[x]][x] - f[tt[x]][x]);
if( flux != 0 )
{
max_flux += flux;
for(int x = n;x!=1;x = tt[x])
{
f[tt[x]][x] +=flux;
f[x][tt[x]] -=flux;
}
}
}
// for(int i=1;i<=n;i++)
// { for(int j=1;j<=n;j++)printf("%d ",f[i][j]); printf("\n"); }
}
printf("%d\n",max_flux);
}
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);
c[x][y] = z;
g[x].push_back(y);
g[y].push_back(x);
}
maxflow();
return 0;
}