Pagini recente » Cod sursa (job #2120242) | Cod sursa (job #2179159) | Cod sursa (job #2440520) | Cod sursa (job #3250201) | Cod sursa (job #2357778)
#include <iostream>
#include <cstdio>
#include <vector>
#include <bitset>
using namespace std;
int n, m, x, y, ct;
vector <int> g[1005];
int c[1005][1005], f[1005][1005];
int fluxmax;
vector <int> q;
bitset <1005> viz;
int t[1005];
bool bfs(){
q.clear();
q.push_back(1);
viz.reset();
viz[1]=0;
while(!q.empty()){
x = q[0];
q.erase(q.begin());
for(int y : g[x]){
if(c[x][y] == f[x][y] || viz[y])
continue;
q.push_back(y);
viz[y]=1;
t[y]=x;
if(y==n)
return 1;
}
}
return 0;
}
int main()
{
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d%d", &n,&m);
for(int i=0;i<m;++i){
scanf("%d%d%d", &x,&y,&ct);
g[x].push_back(y);
g[y].push_back(x);
c[x][y]=ct;
}
while(bfs()){
int fluxmin = 0x3f3f3f3f;
x=n;
while(x!=1){
y=t[x];
fluxmin = min(c[y][x]-f[y][x], fluxmin);
x=y;
}
fluxmax += fluxmin;
x=n;
while(x!=1){
y=t[x];
f[x][y] -= fluxmin;
f[y][x] += fluxmin;
x=y;
}
}
cout<<fluxmax;
return 0;
}