Cod sursa(job #2212015)

Utilizator zvonTutuldunsa Voronokda zvon Data 12 iunie 2018 21:37:58
Problema Flux maxim Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.42 kb
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include"string.h"
#define MAXCAP 110000
using namespace std;

typedef struct edge{
	int source;
	int dest;
	int cap;
	int flow;
	edge (int source, int dest, int cap) {
		this->source = source;
		this->dest = dest;
		this->cap = cap;
		this->flow = 0;	
	}	
};

void clear( std::queue<int> &q )
{
   std::queue<int> empty;
   std::swap( q, empty );
}

ifstream fi("maxflow.in");
ofstream fo("maxflow.out");
int N, M;
vector < vector < edge > > G;

int main() {
	int i, j, x, y, z;
	edge* path[1004];
	fi >> N >> M;
	G.resize(N + 1);
	for (i = 0; i < M; i++) {
		fi >> x >> y >> z;
		G[x].push_back(edge(x, y, z));
	}
	long Flow = 0;
	queue <int> q;
	while (true) {
		clear(q);
		q.push(1);
		memset(path, 0, 1004 * sizeof(edge *));
		while (!q.empty()) {
			int cur = q.front();
			q.pop();
			for (i = 0; i < G[cur].size(); i++) {
				edge e = G[cur][i];
				if (path[e.dest] == 0 && e.dest != 1 && e.cap > e.flow) {
					path[e.dest] = &G[cur][i];
					q.push(e.dest);
				}
			}	
		}
		if (path[N]) {
			int minf = MAXCAP;
			for (edge *e = path[N]; e != NULL; e = path[e->source]) {
				minf = min(minf, e->cap - e->flow);
			}
			for (edge *e = path[N]; e != NULL; e = path[e->source]) {
				e->flow += minf;
			}
			Flow += minf;
		} else
			break;
	}
	fo << Flow;
	fi.close();
	fo.close();
	return 0;	
}