Pagini recente » Cod sursa (job #562600) | Cod sursa (job #1118799) | Cod sursa (job #19677) | Cod sursa (job #1435335) | Cod sursa (job #2739465)
#include <bits/stdc++.h>
using namespace std;
const int INF = (1 << 30);
using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
void BUNA(const string& task = "")
{
if (!task.empty())
freopen((task + ".in").c_str(), "r", stdin),
freopen((task + ".out").c_str(), "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
}
void PA()
{
exit(0);
}
class Graf
{
public:
Graf(const int& _n = 0) : n(_n), G(_n + 1), dad(_n + 1) {
cap = VVI(n + 1, VI(n + 1));
}
inline void AddEdge(int x, int y, int c)
{
G[x].emplace_back(y);
G[y].emplace_back(x);
cap[x][y] = c;
}
inline int MaxFlow()
{
int x, flow, flowmax(0);
while(BFS())
{
for(auto nod: G[n])
{
if(dad[nod] && cap[nod][n])
{
dad[n] = nod;
flow = INF;
x = n;
while(x != 1)
{
flow = min(flow, cap[dad[x]][x]);
x = dad[x];
}
if(flow == INF)
continue;
x = n;
while(x != 1)
{
cap[dad[x]][x] -= flow;
cap[x][dad[x]] += flow;
x = dad[x];
}
flowmax += flow;
}
}
}
return flowmax;
}
private:
inline bool BFS()
{
queue<int> q;
q.push(1);
fill(dad.begin(), dad.end(), 0);
dad[1] = 1;
while(!q.empty())
{
auto x = q.front();
q.pop();
if(x == n)
continue;
for(auto y: G[x])
if(!dad[y] && cap[x][y])
{
dad[y] = x;
q.push(y);
}
}
return dad[n];
}
int n;
VVI G, cap;
VI dad;
};
int n, m;
int main()
{
BUNA("maxflow");
cin >> n >> m;
Graf g(n);
for(int i = 1; i <= m; ++i)
{
int x, y, c;
cin >> x >> y >> c;
g.AddEdge(x, y, c);
}
cout << g.MaxFlow() << '\n';
PA();
}