Pagini recente » Cod sursa (job #996426) | Cod sursa (job #2754494) | Cod sursa (job #2212802) | Cod sursa (job #622531) | Cod sursa (job #743030)
Cod sursa(job #743030)
#include<iostream>
#include<fstream>
#include<vector>
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];
ifstream in("maxflow.in");
ofstream out("maxflow.out");
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()
{
int i, flow, fmin, x, y, z, nod;
in >> n >> m;
for(i=1; i<=m; ++i){
in >> 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;
return 0;
}