Pagini recente » Cod sursa (job #1962515) | Cod sursa (job #2573503) | Cod sursa (job #2413020) | Cod sursa (job #835101) | Cod sursa (job #1955487)
// Flux Maxim.cpp : Defines the entry point for the console application.
//
#include "iostream"
#include "fstream"
#include "vector"
#include <cstring>
#define NMAX 1005
#define INF 1000000000
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int capacitate[NMAX][NMAX], flux[NMAX][NMAX], frezidual[NMAX][NMAX], n, m;
int Father[NMAX], frezmin, MinFather[NMAX];
vector < int > a[NMAX];
inline int min(const int & a, const int & b) {
if (a < b) return a;
return b;
}
bool DF(const int & nod, const int & father, const int & destination) {
Father[nod] = father;
if (nod == destination) return true;
for (int i = 0; i < a[nod].size(); i++) {
int next = a[nod][i];
if (frezidual[nod][next] > 0 && Father[next] == 0) {
MinFather[next] = min(MinFather[nod], frezidual[nod][next]);
if (DF(next, nod, destination)) return true;
}
}
return false;
}
void UpdateFluxRezidual(const int & nod, const int & value, const int & source) {
if (nod == source) return;
frezidual[Father[nod]][nod] -= value;
UpdateFluxRezidual(Father[nod], value, source);
}
int maxflow()
{
int source = 1, destination = n;
int flow = 0;
vector < int > dfs;
bool sw;
do {
memset(Father, 0, sizeof(Father));
MinFather[source] = INF;
sw = DF(source, source, destination);
if (sw) {
flow += MinFather[destination];
UpdateFluxRezidual(destination, MinFather[destination], source);
}
} while (sw);
return flow;
}
int main()
{
f >> n >> m;
for (int i = 0; i < m; i++) {
int x, y, c;
f >> x >> y >> c;
a[x].push_back(y);
capacitate[x][y] = c;
frezidual[x][y] = c;
frezidual[y][x] = -c;
}
g << maxflow();
return 0;
}