Pagini recente » Cod sursa (job #454793) | Cod sursa (job #193351) | Cod sursa (job #2044208) | Cod sursa (job #1200501) | Cod sursa (job #769871)
Cod sursa(job #769871)
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define MAX 1001
#define INF 0xffffff
vector<int>g[MAX];
int n,m,c[MAX][MAX],f[MAX][MAX],tt[MAX],cd[MAX];
bool was[MAX];
bool drum(){
memset(was,0,sizeof(was));
int p , u ,x ,y;
cd[p = u = 1] = 1;
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;
tt[y] = x;
if(y == n) return 1;
cd[++u] = y;
}
}
}
return 0;
}
void maxflow(){
int flux_m = 0, flux;
while( drum() )
{
flux = INF;
for(int x = n; x!= 1;x = tt[x]) flux = min(flux, c[tt[x]][x]-f[tt[x]][x]);
for(int x = n;x != 1;x = tt[x])
{
f[tt[x]][x] += flux;
f[x][tt[x]] -= flux;
}
flux_m += flux;
}
printf("%d\n",flux_m);
}
int main(){
int x,y,z;
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&z);
g[x].push_back(y);
g[y].push_back(x);
c[x][y] = z;
}
maxflow();
return 0;
}