Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Maximum Flow

A flow network is a directed graph which has a $source$ and a $sink$. In a flow network, each edge $(u, v)$ has a capacity $c(u, v)$. Each edge receives a flow, but the amount of flow on the edge can not exceed the corresponding capacity. Find the maximum flow from the $source$ to the $sink$.

Input

A flow network is given in the following format.

$|V|\;|E|$

$u_0\;v_0\;c_0$

$u_1\;v_1\;c_1$

:

$u_{|E|-1}\;v_{|E|-1}\;c_{|E|-1}$

$|V|$, $|E|$ is the number of vertices and edges of the flow network respectively. The vertices in $G$ are named with the numbers 0, 1,..., $|V|-1$. The source is 0 and the sink is $|V|-1$.

$u_i$, $v_i$, $c_i$ represent $i$-th edge of the flow network.
A pair of $u_i$ and $v_i$ denotes that there is an edge from $u_i$ to $v_i$ and $c_i$ is the capacity of $i$-th edge.

Output

Print the maximum flow.

Constraints

$2 \leq |V| \leq 100$

$1 \leq |E| \leq 1000$

$0 \leq c_i \leq 10000$

Sample Input 1

4 5
0 1 2
0 2 1
1 2 1
1 3 1
2 3 2

Sample Output 1

3

Solution in C++14 (judge verdict: Time Limit Exceeded)

#include<iostream>
#include<vector>
#include<string>
#include<array>
#include<algorithm>
#include<list>
#include<cmath>
#include<iomanip>
#include<queue>
#include<functional>
#include<climits>
#include<iterator>
using namespace std;

const double pi=4*atan(1.0);
using cMatrixRepEdges=std::vector<std::vector<int>>;
pair<vector<int>,int> solve_by_EdmondsKarp_helper(const cMatrixRepEdges& ResidualNetwork,int source,int sink){
	if(ResidualNetwork.empty())return make_pair(vector<int>(),0);

	vector<bool> IsVisit(ResidualNetwork.size(),false);
	vector<int> Parents(ResidualNetwork.size(),-1);
	queue<pair<int,int>> Q;
	Q.push(make_pair(source,INT_MAX));
	IsVisit[source]=true;

	int ans=0;
	while(!Q.empty()){
		auto now=Q.front();
		Q.pop();
		if(now.first==sink){
			ans=now.second;
			break;
		}

		for(int i=0;i<ResidualNetwork.size();++i){
			if(ResidualNetwork[now.first][i]>0 && !IsVisit[i]){
				IsVisit[i]=true;
				Parents[i]=now.first;
				Q.push(make_pair(i,min(now.second,ResidualNetwork[now.first][i])));
			}
		}
	}

	return make_pair(Parents,ans);
}
cMatrixRepEdges solve_by_EdmondsKarp(const cMatrixRepEdges& FlowNetwork,int source,int sink){
	if(FlowNetwork.empty() || source<0 || FlowNetwork.size()<=source || sink<0 || FlowNetwork.size()<=sink)return cMatrixRepEdges();

	cMatrixRepEdges ResidualNetwork=FlowNetwork;
	cMatrixRepEdges AnsFlow(FlowNetwork.size(),vector<int>(FlowNetwork.size(),0));

	while(1){
		auto ans=solve_by_EdmondsKarp_helper(ResidualNetwork,source,sink);
		//source??????sink???????????????????????????????????£???
		if(ans.first[sink]<0)break;

		int to=sink;
		int from=ans.first[sink];
		while(to!=source){
			if(FlowNetwork[from][to]>0){
				AnsFlow[from][to]+=ans.second;
			}
			else{
				AnsFlow[to][from]-=ans.second;
				swap(from,to);
			}
			ResidualNetwork[from][to]=FlowNetwork[from][to]-AnsFlow[from][to];
			ResidualNetwork[to][from]=AnsFlow[from][to];

			to=from;
			from=ans.first[from];
		}
	}

	return AnsFlow;
}


int main() {
	int N,M;
	cin>>N>>M;
	cMatrixRepEdges Edges(N,vector<int>(N,0));
	for(int i=0;i<M;++i){
		int u,v,w;
		cin>>u>>v>>w;

		Edges[u][v]=w;
		//Edges[u].push_back(v);
	}

	auto ans=solve_by_EdmondsKarp(Edges,0,N-1);
	
	int flow=0;
	for(const auto& val:ans[0]){
		flow+=val;
	}

	for(int i=0;i<N;++i){
		flow-=ans[i][0];
	}

	cout<<flow<<endl;

	//system("pause");
	return 0;
}