Pagini recente » Cod sursa (job #2469992) | Cod sursa (job #2122805) | Cod sursa (job #440548) | Cod sursa (job #938642) | Cod sursa (job #1516353)
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
const int nmax = 1000;
const int inf = (1LL<<31)-1;
int g[nmax+5][nmax+5];
vector <int> v[nmax+5];
queue <int> q;
int pred[nmax+5];
int n, m;
int s, d;
int bfs()
{
memset(pred, -1, sizeof(pred));
pred[s] = -2;
while(!q.empty())q.pop();
q.push(s);
while(!q.empty() && pred[d]==-1)
{
int tmp = q.front();
q.pop();
for(int i=0; i<v[tmp].size(); i++)
{
int V = v[tmp][i];
if(g[tmp][V] && pred[V]==-1)
{
q.push(V);
pred[V] = tmp;
}
}
}
int pos = d, Min = inf;
if(pred[pos]==-1)return -1;
while(pred[pos]>0)
{
Min = min(Min, g[pred[pos]][pos]);
pos = pred[pos];
}
pos = d;
while(pred[pos]>0)
{
g[pos][pred[pos]] += Min;
g[pred[pos]][pos] -= Min;
pos = pred[pos];
}
return Min;
}
int flux()
{
int ans = 0;
while(true)
{
int Min = bfs();
if(Min == -1)break;
ans+=Min;
}
return ans;
}
int main()
{
freopen("maxflow.in", "r", stdin);
freopen("maxflow.out", "w", stdout);
scanf("%d%d", &n, &m);
s=1;d=n;
for(int i=0; i<m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
g[x][y] += z;
v[x].push_back(y);
v[y].push_back(x);
}
printf("%d\n", flux());
return 0;
}