Pagini recente » Cod sursa (job #2189267) | Cod sursa (job #3004451) | Cod sursa (job #1957601) | Cod sursa (job #2159342) | Cod sursa (job #1911191)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int n, m, i;
int residue[2000][2000];
bool bfs(vector<list<pair<int, int> > >adjacencyList, int s, int t, int parent[]){
vector<bool>visited(n+1, false);
queue<int> q;
visited[s] = true;
q.push(s);
parent[s] = -1;
while(!q.empty()){
int u = q.front();
q.pop();
for(pair<int, int>aux: adjacencyList[u]){
int v = aux.first;
if(!visited[v] && residue[u][v] > 0){
q.push(v);
parent[v] = u;
visited[v] = true;
}
}
}
return (visited[t] == true);
}
int main()
{
f>>n>>m;
vector<list<pair<int, int> > >adjacencyList(n+1);
for(i=1; i<=m; i++){
int x, y, z;
f>>x>>y>>z;
adjacencyList[x].push_back(make_pair(y, z));
residue[x][y] = z;
}
int parent[n+1];
int max_flow = 0;
int v, u;
while(bfs(adjacencyList, 1, n, parent)){
int path_flow = INF;
for(v=n; v!=1; v=parent[v]){
u = parent[v];
path_flow = min(path_flow, residue[u][v]);
}
for (v=n; v != 1; v=parent[v])
{
u = parent[v];
residue[u][v] -= path_flow;
residue[v][u] += path_flow;
}
max_flow += path_flow;
}
g<<max_flow;
return 0;
}