Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**------------------------
- Author : NikolaP
- Compiler : C++
- -------------------------*/
- //{ Setup
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- typedef long double ld;
- typedef vector<int> vi;
- const int inf = INT_MAX;
- const bool debug = 0;
- //}
- struct EdmondKarp{
- int n;
- vector<vi> capacity;
- vector<vi> flowpassed;
- vi parents;
- vi currentpathcapacity;
- vector<vi> graph;
- EdmondKarp(int _n){
- n = _n;
- capacity.assign(n,vi(n,0));
- flowpassed.assign(n,vi(n,0));
- parents.assign(n,-1);
- currentpathcapacity.assign(n,0);
- graph.resize(n);
- }
- void InputGraph(int edges){
- int from, to, c;
- for(int i = 0; i < edges; ++i){
- cin >> from >> to >> c;
- capacity[from][to] = c;
- graph[from].push_back(to);
- graph[to].push_back(from);
- }
- }
- int bfs(int start, int end){
- parents.assign(n,-1);
- currentpathcapacity.assign(n,0);
- queue<int> q;
- q.push(start);
- parents[start] = -2;
- currentpathcapacity[start] = inf;
- while(!q.empty()){
- int current = q.front();
- q.pop();
- for(int i = 0; i < graph[current].size(); ++i){
- int to = graph[current][i];
- if(parents[to] == -1){
- if(capacity[current][to] - flowpassed[current][to] > 0){
- parents[to] = current;
- currentpathcapacity[to] = min(currentpathcapacity[current] , capacity[current][to] - flowpassed[current][to]);
- if(to == end) return currentpathcapacity[end];
- q.push(to);
- }
- }
- }
- }
- return 0;
- }
- int Solution(int start, int end){
- int maxflow = 0;
- for(;;){
- int flow = bfs(start,end);
- if(flow == 0) break;
- maxflow += flow;
- int current = end;
- while(current != start){
- int previous = parents[current];
- flowpassed[previous][current] += flow;
- flowpassed[current][previous] -= flow;
- current = previous;
- }
- }
- return maxflow;
- }
- };
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.precision(8);
- //cout << fixed;
- int n,m;
- cin >> n >> m;
- EdmondKarp ek = EdmondKarp(n);
- int start,end;
- cin >> start >> end;
- ek.InputGraph(m);
- cout << ek.Solution(start,end);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment