Pagini recente » Cod sursa (job #465599) | Cod sursa (job #2565588) | Cod sursa (job #2651376) | Cod sursa (job #949237) | Cod sursa (job #2786977)
#include <iostream>
#include <climits>
#include <fstream>
#include <vector>
#include <queue>
#define VMAX 1000
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
vector <int> adj[VMAX];
int V, E, x, y;
int flow[VMAX][VMAX], capacity[VMAX][VMAX];
int level[VMAX], parent[VMAX], start[VMAX];
bool BFS()
{
queue <int> q;
for (int i = 0; i < V; ++i) level[i] = -1;
q.push(0);
level[0] = 0;
parent[0] = -1;
while (!q.empty())
{
int u = q.front();
q.pop();
for (auto w:adj[u])
if (level[w] == -1 && flow[u][w] < capacity[u][w])
{
level[w] = level[u] + 1;
parent[w] = u;
q.push(w);
}
}
return level[V - 1] != -1;
}
int DFS(int u, int current_flow)
{
if (u == V - 1) return current_flow;
for (; start[u] < adj[u].size(); ++start[u])
{
int w = adj[u][start[u]];
if (level[w] == level[u] + 1 && flow[u][w] < capacity[u][w])
{
int curr_flow = min(current_flow, capacity[u][w] - flow[u][w]);
int temp_flow = DFS(w, curr_flow);
if (temp_flow > 0)
{
flow[u][w] += temp_flow;
flow[w][u] -= temp_flow;
return temp_flow;
}
}
}
return 0;
}
int main()
{
fin >> V >> E;
while (E--)
{
fin >> x >> y >> capacity[x - 1][y - 1];
adj[x - 1].push_back(y - 1);
adj[y - 1].push_back(x - 1);
}
int max_flow = 0;
while (BFS())
{
for (int i = 0; i < V; ++i) start[i] = 0;
while (int current_flow = DFS(0, INT_MAX))
max_flow += current_flow;
}
fout << max_flow;
fin.close();
fout.close();
return 0;
}