Pagini recente » Cod sursa (job #2978371) | Cod sursa (job #1677923) | Cod sursa (job #3143190) | Cod sursa (job #1477669) | Cod sursa (job #2984154)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f ("apa.in");
ofstream g ("apa.out");
const long long INF = __LONG_LONG_MAX__;
const int SIZE = 1001;
int n, m;
long long capacity[SIZE][SIZE];
vector <pair <int, int > > adj[SIZE];
vector <int> parent;
long long BFS(int s, int t)
{
fill(parent.begin(), parent.end(), -1);
parent[s] = -2;
queue <pair <long long, long long> > Q;
Q.push(make_pair(s, INF));
while (Q.empty() == false)
{
long long cur = Q.front().first;
long long flow = Q.front().second;
Q.pop();
for (unsigned int i = 0; i < adj[cur].size(); i++)
{
long long next = adj[cur][i].first;
if (parent[next] == -1 && capacity[cur][next] != 0)
{
parent[next] = cur;
long long newFlow = min(flow, capacity[cur][next]);
Q.push(make_pair(next, newFlow));
if (next == t)
{
return newFlow;
}
}
}
}
return 0;
}
long long MaxFlow(int s, int t)
{
long long maxFlow = 0, newFlow;
parent.resize(n + 1);
while ((newFlow = BFS(s, t)) != 0)
{
maxFlow += newFlow;
int cur = t;
while (cur != s)
{
int prev = parent[cur];
capacity[prev][cur] -= newFlow;
capacity[cur][prev] += newFlow;
cur = prev;
}
}
return maxFlow;
}
void Read()
{
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, z;
cin >> x >> y >> z;
adj[x].push_back(make_pair(y, z));
adj[y].push_back(make_pair(x, z));
capacity[x][y] += z;
}
}
int main()
{
ios :: sync_with_stdio(0);
cin.tie(0);
Read();
cout << MaxFlow(1, n);
}