Pagini recente » Cod sursa (job #1703821) | Clasament cnitv_baraj_2 | Cod sursa (job #2472316) | Cod sursa (job #2702657) | Cod sursa (job #1180743)
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <cmath>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <list>
#include <bitset>
#include <climits>
#include <ctime>
#include <cassert>
#include <iomanip>
using namespace std;
const string file = "maxflow";
const string inputF = file + ".in";
const string outputF = file + ".out";
const double epsilon = 1e-7;
#define LL long long
#define ULL unsigned long long
#define MOD1 666013
#define MOD2 666019
#define MOD3 1999999973
#define base 73
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<bool> vb;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<string> vs;
typedef pair<int, int> ii;
typedef pair<long long, long long> ll;
typedef vector<ii> vii;
typedef vector<ll> vll;
#define all(V) V.begin(), V.end()
#define allr(V) V.rbegin(), V.rend()
#define for_c_it(container, it) for (auto it : container)
#define present(container, element) (container.find(element) != container.end())
#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define zeroes(x) ((x ^ (x - 1)) & x)
int N, M, maxFlow;
vvi graph, cap;
vi prec;
bool BFS(int source, int dest);
int main() {
#ifndef INFOARENA
freopen("input.txt", "r", stdin);
#else
freopen(inputF.c_str(), "r", stdin);
freopen(outputF.c_str(), "w", stdout);
#endif
int i, from, to, c;
scanf("%d %d", &N, &M);
graph.resize(N);
prec.resize(N);
cap.resize(N, vector<int>(N));
for (i = 0; i < M; ++i) {
scanf("%d %d %d", &from, &to, &c);
--from; --to;
cap[from][to] = c;
graph[from].pb(to);
graph[to].pb(from);
}
while (BFS(0, N - 1)) {
for (auto from:graph[N - 1]) {
if (prec[from] != -1 && cap[from][N - 1] > 0) {
prec[N - 1] = from;
int minFlow = INT_MAX;
for (to = N - 1; to != 0; to = prec[to]) {
minFlow = min(minFlow, cap[prec[to]][to]);
}
for (to = N - 1; to != 0; to = prec[to]) {
cap[prec[to]][to] -= minFlow;
cap[to][prec[to]] += minFlow;
}
maxFlow += minFlow;
}
}
}
printf("%d\n", maxFlow);
return 0;
}
bool BFS(int source, int dest) {
int from;
deque <int> deq;
fill(all(prec), -1);
for (deq.pb(source); !deq.empty(); deq.pop_front()) {
from = deq.front();
if (from == dest) {
return true;
}
for (auto to:graph[from]) {
if (cap[from][to] > 0 && prec[to] == -1) {
prec[to] = from;
deq.pb(to);
}
}
}
return false;
}