Pagini recente » Cod sursa (job #1044643) | Cod sursa (job #2246795) | Cod sursa (job #2230850) | Cod sursa (job #2327964) | Cod sursa (job #1496676)
#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];
class FLUX
{
private:
int n, m;
int s, d;
int pred[nmax+5];
vector <int> v[nmax+5];
queue <int> q;
int bfs()
{
memset(pred, 0, sizeof(pred));
pred[s] = -1;
while(!q.empty())q.pop();
q.push(s);
while(!q.empty() && pred[d]==0)
{
int tmp = q.front();
q.pop();
for(int i=0; i<v[tmp].size(); i++)
{
if(g[tmp][v[tmp][i]] && pred[v[tmp][i]]==0)
{
q.push(v[tmp][i]);
pred[v[tmp][i]] = tmp;
}
}
}
int pos = d, Min = inf;
if(pred[pos]<=0)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;
}
public:
FLUX(int a, int b)
{
n = a, m = b;
s = 1, d = n;
}
void push(int x, int y, int c)
{
v[x].push_back(y);
v[y].push_back(x);
}
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);
int n, m;
scanf("%d%d", &n, &m);
FLUX G(n, m);
for(int i=0; i<m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
g[x][y] += z;
G.push(x, y, z);
}
printf("%d\n", G.flux());
return 0;
}