Pagini recente » Cod sursa (job #1126782) | Cod sursa (job #2883841) | Cod sursa (job #477483) | Cod sursa (job #174862) | Cod sursa (job #2954607)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define max_capacity 110001
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n, m, x, y, cap, max_flow;
int capacity[1001][1001], f[1001][1001];
vector<int>parent, visited;
vector<int>adjacency_list[1001];
queue<int>q;
int bfs(){
visited.assign(n+1, 0);
parent.assign(n+1, 0);
while(!q.empty()){
q.pop();
}
q.push(1);
visited[1]=1;
parent[1]=1;
while(!q.empty()){
int node=q.front();
q.pop();
if(node==n){
visited[n]=1;
return visited[n];
}
for(auto neighbour:adjacency_list[node]){
if(capacity[node][neighbour]==f[node][neighbour] || visited[neighbour])
continue;
else {
q.push(neighbour);
parent[neighbour]=node;
visited[neighbour]=1;
}
}
}
return visited[n];
}
int main() {
fin>>n>>m;
for(int i=1; i<=m; i++){
fin>>x>>y>>cap;
capacity[x][y] = cap;
adjacency_list[x].push_back(y);
adjacency_list[y].push_back(x);
}
while(bfs()){
for(auto node: adjacency_list[n]){
if(f[node][n]==capacity[node][n] || !visited[node])
continue;
parent[n]=node;
int flow=max_capacity;
int destination=n;
while(destination!=1){
flow=min(flow, capacity[parent[destination]][destination]-f[parent[destination]][destination]);
destination=parent[destination];
}
destination=n;
if(flow==0) continue;
while(destination!=1){
/*capacity[parent[destination]][destination]-=flow;
capacity[destination][parent[destination]]+=flow;*/
f[parent[destination]][destination]+=flow;
f[destination][parent[destination]]+=flow;
destination=parent[destination];
}
max_flow+=flow;
}
}
fout<<max_flow;
return 0;
}