Pagini recente » Cod sursa (job #332209) | Cod sursa (job #2273374) | Cod sursa (job #726352) | Cod sursa (job #989057) | Cod sursa (job #2954617)
#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){
continue;
}
for(auto neighbour:adjacency_list[node]){
if(capacity[node][neighbour]==f[node][neighbour] || visited[neighbour])
continue;
if(visited[neighbour]==0 && capacity[node][neighbour]>0) {
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;
for(node=n; node!=1; node=parent[node])
flow=min(flow,capacity[parent[node]][node]-f[parent[node]][node]);
/*while(destination!=1){
flow=min(flow, capacity[parent[destination]][destination]-f[parent[destination]][destination]);
destination=parent[destination];
}*/
//destination=n;
if(flow==0) continue;
for(node=n; node!=1; node=parent[node]){
f[parent[node]][node]+=flow;
f[node][parent[node]]+=flow;
}
/*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;
}