Pagini recente » Cod sursa (job #2784160) | Cod sursa (job #119371) | Cod sursa (job #1843421) | Cod sursa (job #2542942) | Cod sursa (job #2387801)
#include <bits/stdc++.h>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
const int DIM = 1007;
vector <int> v[DIM];
vector <int> parent;
const int INF = 1e9;
int capacity[DIM][DIM];
bool start[DIM];
bool finish[DIM];
int bfs(int s, int t)
{
fill(parent.begin(), parent.end(), -1);
parent[s] = -2;
queue <pair <int, int> > q;
q.push({s, INF});
while(!q.empty())
{
int nod = q.front().first;
int flow = q.front().second;
q.pop();
for(int urm : v[nod])
{
if(parent[urm] == -1 && capacity[nod][urm])
{
parent[urm] = nod;
int new_flow = min(flow, capacity[nod][urm]);
if(urm == t)
return new_flow;
q.push({urm, new_flow});
}
}
}
return 0;
}
int maxFlow(int s, int t)
{
int flow = 0;
int new_flow = 0;
while(new_flow = bfs(s, t))
{
flow += new_flow;
int curr = t;
while(curr != s)
{
int prev = parent[curr];
capacity[prev][curr] -= new_flow;
capacity[curr][prev] += new_flow;
curr = prev;
}
}
return flow;
}
int main()
{
int n, m;
in >> n >> m;
int s = 0;
int t = 0;
for(int i = 1; i <= m; i++)
{
int x, y, c;
in >> x >> y >> c;
v[x].push_back(y);
v[y].push_back(x);
capacity[x][y] += c;
}
parent.resize(n + 2);
out << maxFlow(1, n);
}