Cod sursa(job #2620234)

Utilizator ViAlexVisan Alexandru ViAlex Data 28 mai 2020 16:41:54
Problema Flux maxim Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.54 kb
#include<bits/stdc++.h>
using namespace std;
 
 
ifstream in("maxflow.in");
ofstream out("maxflow.out");
 
 
int num_nodes,num_edges,sink;
long long flux[1000][1000];
long long capacity[1000][1000];
int pred[1000];
bool visited[1000];
vector<int> graph[1000];
 
 
 
void read(){
	in>>num_nodes>>num_edges;
	int a,b,c;
	sink=num_nodes-1;
	for(int i=0;i<num_edges;i++){
		in>>a>>b>>c;
		capacity[a-1][b-1]+=c;
		capacity[b-1][a-1]=0;
		graph[a-1].push_back(b-1);
		graph[b-1].push_back(a-1);
	}
}
 
 
 
void solve(){
	long long max_flux=0;
	bool found_path=false;
	do{
		memset(pred,-1,1000*sizeof(int));
		memset(visited,0,1000*sizeof(bool));
		found_path=false;
		std::deque<int> nodes;
		visited[0]=true;
		nodes.push_back(0);
		while(nodes.size()){
			int here=nodes.front();
			if(here==sink){
				found_path=true;
				break;
			}
			nodes.pop_front();
			for(int ng:graph[here]){
				if(!visited[ng] && flux[here][ng]<capacity[here][ng]){
					pred[ng]=here;
					nodes.push_back(ng);
					visited[ng]=true;
				}
			}
		}
 
		if(found_path){
			long long  min_cap=1e12;
			int here=sink;
			while(pred[here]!=-1){
				int predecessor=pred[here];
				long long remaining=capacity[predecessor][here]-flux[predecessor][here];
				min_cap=min(min_cap,remaining);
				here=pred[here];
			}
			here=sink;
			while(pred[here]!=-1){
				int predecessor=pred[here];
				flux[predecessor][here]+=min_cap;
				flux[here][predecessor]-=min_cap;
				here=pred[here];
			}
			max_flux+=min_cap;
		}
	}
	while(found_path);
	out<<max_flux;
}
 
int main(){
	read();
	solve();
	return 0;
}