Pagini recente » Cod sursa (job #1986082) | Cod sursa (job #2829385) | Cod sursa (job #2904795) | Cod sursa (job #2297049) | Cod sursa (job #743032)
Cod sursa(job #743032)
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 1 << 10;
const int inf = 0x3f3f3f3f;
int c[maxn][maxn], f[maxn][maxn], tt[maxn], m, n, cd[maxn], viz[maxn];
vector <int> g[maxn];
bool BF()
{
int i, j, nod, V;
cd[0]=1;
cd[1]=1;
memset(viz, 0, sizeof(viz));
viz[1]=1;
for(i=1; i<=cd[0]; ++i){
nod = cd[i];
if(nod == n)
continue;
for(j=0; j<g[nod].size(); ++j){
V = g[nod][j];
if(c[nod][V] == f[nod][V] || viz[V])
continue;
viz[V] = 1;
cd[ ++cd[0] ] = V;
tt[V] = nod;
}
}
return viz[n];
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
int i, flow, fmin, x, y, z, nod;
//in >> n >> m;
scanf("%d %d", &n, &m);
for(i=1; i<=m; ++i){
//in >> x >> y >> z;
scanf("%d %d %d", &x, &y, &z);
c[x][y]+=z;
g[x].push_back(y);
g[y].push_back(x);
}
for(flow=0; BF();)
for(i=0; i<g[n].size(); ++i){
nod = g[n][i];
if(c[nod][n] == f[nod][n] || !viz[nod])
continue;
tt[n] = nod;
fmin = inf;
for(nod = n; nod != 1; nod = tt[nod]){
f[ tt[nod] ][nod] += fmin;
f[nod][ tt[nod] ] -= fmin;
}
flow += fmin;
}
//out << flow;
printf("%d", flow);
return 0;
}