Pagini recente » Cod sursa (job #1958994) | Cod sursa (job #1104938) | Cod sursa (job #1643221) | Cod sursa (job #2945790) | Cod sursa (job #3328011)
#include <bits/stdc++.h>
using namespace std;
int cap[1005][1005], n, m;
vector <int> v[1005];
int bfs(int s, int t, vector <int> &parent)
{
fill(parent.begin(), parent.end(), -1);
deque <pair<int, int>> q;
q.push_back({s, 1e9});
parent[s] = -2;
while(!q.empty())
{
int nod = q.front().first, flow = q.front().second;
q.pop_front();
for(int i = 0; i < v[nod].size(); i ++)
{
int next = v[nod][i];
int capacity = cap[nod][next];
if(capacity && parent[next] == -1)
{
parent[next] = nod;
int min_flow = min(flow, capacity);
if(next == t)
return min_flow;
q.push_back({next, min_flow});
}
}
}
return 0;
}
int flow(int s, int t)
{
int max_flow = 0, new_flow = 0;
vector <int> parent(n + 1);
while(new_flow = bfs(s, t, parent))
{
max_flow += new_flow;
int nod = t;
while(nod != s)
{
int prev = parent[nod];
cap[prev][nod] -= new_flow;
cap[nod][prev] += new_flow;
nod = parent[nod];
}
}
return max_flow;
}
int main()
{
ifstream f("maxflow.in");
ofstream g("maxflow.out");
f >> n >> m;
for(int i = 1; i <= m; i ++)
{
int x, y, c;
f >> x >> y >> c;
v[x].push_back(y);
v[y].push_back(x);
cap[x][y] = c;
}
g << flow(1, n);
return 0;
}