Cod sursa(job #1241483)

Utilizator alexnekiNechifor Alexandru alexneki Data 13 octombrie 2014 17:30:21
Problema Flux maxim Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 2.15 kb
#include <iostream>
#include <fstream>
#include <vector>

int const nMax = 1001;
int const capMax = 110010;

int n, m; //number of vertices and edges, respectively
int source, drain;
int cap[nMax][nMax], flux[nMax][nMax];
int vis[nMax]; //has the node been visited or not
int pred[nMax]; //for reconstructing the road
int maxUpdate; //how much should the flux increase on one road

using namespace std;

vector <int> x[nMax];

int bf() {
	int stack[nMax], nSt;

	for(int i=0; i<n; i++) {
		vis[i] = 0;
		pred[i] = -1;
	}

	nSt = 1;
	stack[0] = source;
	vis[source] = 1;
	
	int k = 0;
	while(k < nSt) {
		//cout<<"Stack "<<stack[k]<<" "<<pred[stack[k]]<<endl;
		int xx = stack[k];
		for(int i=0; i<x[i].size(); i++) {
			int yy = x[xx][i];
			int dif = cap[xx][yy] - flux[xx][yy];
			if(vis[yy]==0 && dif > 0)  { //add in stack
				stack[nSt] = yy;
				vis[yy] = 1;
				pred[yy] = xx;
				nSt++;
			}
		}
		k++;
	}
	return vis[drain];
}

void updateFlux(int k, int value) { //how much should the flux increase on this road
	if(pred[k]>=0) {
		flux[pred[k]][k] += value;
		flux[k][pred[k]] -= value;
		updateFlux(pred[k], value);
		//cout<<"("<<pred[k]<<","<<k<<","<<flux[pred[k]][k]<<")"<<endl;
	}
}

void computeMaxUpdate(int k) {
	if(pred[k]>=0) {
		int dif = cap[pred[k]][k] - flux[pred[k]][k];
		if(maxUpdate > dif) 
			maxUpdate = dif;
		computeMaxUpdate(pred[k]);
	}
}

void printWay(int k) {
	if(pred[k]>=0) 
		printWay(pred[k]);
	cout<<k<<" ";
}

int main() {
	ifstream in("maxflow.in");
	streambuf* cinbuf = cin.rdbuf(); //save old buffer
	cin.rdbuf(in.rdbuf());


	ofstream out("maxflow.out");
	streambuf* coutbuf = cout.rdbuf(); //save old buffer
	cout.rdbuf(out.rdbuf());

	cin>>n>>m;
	int u, v, cost;
	for(int i=0; i<m; i++) {
		cin>>u>>v>>cost;
		cap[u-1][v-1] = cost;
		x[u-1].push_back(v-1);
		x[v-1].push_back(u-1);
	}	
	source = 0;
	drain = n-1;
	
	while(bf()==1) {
		maxUpdate = capMax;
		computeMaxUpdate(drain);
		//printWay(drain);
		//cout<<"-> "<<maxUpdate[drain]<<endl;
		updateFlux(drain, maxUpdate);
		//cout<<endl;
	}
	
	int totalFlux = 0;
	for(int i=0; i<n; i++)
		totalFlux += flux[source][i];
	cout<<totalFlux<<endl;
	
	cin.rdbuf(cinbuf);
	cout.rdbuf(coutbuf);
	return 0;
}