Pagini recente » Cod sursa (job #525079) | Monitorul de evaluare | Monitorul de evaluare | Monitorul de evaluare | Cod sursa (job #623364)
Cod sursa(job #623364)
#include<fstream>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
vector<vector<int> > mat(1001);
int f[1001][1001],cap[1001][1001],up[1001];
int n,m;
inline int bfs(int s, int d)
{
int xx, sz, x, i;
memset(up, -1, sizeof(up));
queue<int> q;
q.push(s);
up[s]=0;
while( !q.empty() )
{
xx=q.front();
for (sz = mat[xx].size(), i = 0; i < sz; ++i)
if (up[x = mat[xx][i]] == -1 && f[xx][x] < cap[xx][x])
{
q.push(x);
up[x] = xx;
if (x == d)
return 1;
}
q.pop();
}
return 0;
}
int max_flow()
{
int i, j, r, cnt = 0;
while(bfs(1,n))
{
for (i = 1; i < n; ++i)
if (up[i] != -1 && f[i][n] < cap[i][n])
{
r = cap[i][n] - f[i][n];
for (j = i; j != 1 && r > 0; j = up[j])
r = min(r, cap[up[j]][j] - f[up[j]][j]);
if (r == 0)
continue;
f[i][n] += r; f[n][i] -= r;
for (j = i; j != 1; j = up[j])
f[up[j]][j] += r, f[j][up[j]] -= r;
cnt += r;
}
}
return cnt;
}
int main()
{
ifstream f("maxflow.in");
ofstream g("maxflow.out");
f>>n>>m;
int x,y;
for(;m;--m)
{
f>>x>>y;
mat[x].push_back(y);
mat[y].push_back(x);
f>>cap[x][y];
}
g<<max_flow();
}