Pagini recente » Cod sursa (job #1416781) | Cod sursa (job #2289903) | Cod sursa (job #772399) | Cod sursa (job #990662) | Cod sursa (job #2730471)
#include<iostream>
#include<vector>
#include<fstream>
#include<queue>
#include<bits/stdc++.h>
#define NMAX 1005
#define MMAX 5005
#define inf 0x3f3f3f3f
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
vector<int> G[NMAX];
int c[NMAX][NMAX], lvl[NMAX];
int n, m;
int flxmax = 0;
int src, snk;
void read()
{
f >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, cap;
f >> x >> y >> cap;
G[x].push_back(y);
G[y].push_back(x);
c[x][y] = cap;
}
}
int bfs()
{
memset(lvl, 0, sizeof(lvl));
queue<int> q;
q.push(src);
lvl[src] = 1;
while (!q.empty())
{
int extract = q.front();
q.pop();
for(auto it: G[extract])
if (lvl[it] == 0 && c[extract][it] > 0)
{
lvl[it] = lvl[extract] + 1;
q.push(it);
}
}
return lvl[snk];
}
int dinic(int nod, int cap)
{
int flux_nod = 0,current_flux;
if (cap == 0)
return 0;
if (nod == snk)
return cap;
for (auto it : G[nod])
if (lvl[it] == lvl[nod] + 1 && c[nod][it] > 0)
{
current_flux = dinic(it, min(cap - flux_nod, c[nod][it]));
c[nod][it] -= current_flux;
c[it][nod] += current_flux;
flux_nod += current_flux;
}
return flux_nod;
}
int main()
{
read();
src = 1, snk = n;
while (bfs())
flxmax += dinic(src, inf);
g << flxmax;
return 0;
}