Pagini recente » Cod sursa (job #1825976) | Cod sursa (job #861564) | Cod sursa (job #2854637) | Cod sursa (job #221826) | Cod sursa (job #3304544)
#include <bits/stdc++.h>
using namespace std;
#define ST_DIO 0
#if ST_DIO
#define fin cin
#define fout cout
#else
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
#endif // ST_DIO
int n, m, i, tata[5002], rasp;
int x, y, st, sf;
vector<int> gr[5002];
int lim[5002][5002];
int flux[5002][5002];
static inline bool Verif(int start = st) {
memset(tata, 0, sizeof(tata));
queue<int> q;
q.push(start);
tata[start] = -1;
bool ok = false;
while(!q.empty()) {
int nod = q.front();
q.pop();
if(nod == sf) ok = true;
for(int urm : gr[nod]) {
if(tata[urm] != 0 || lim[nod][urm] <= flux[nod][urm]) continue;
tata[urm] = nod;
q.push(urm);
}
}
return ok;
}
static inline void CautaFlux() {
while(Verif()) {
for(int ant : gr[sf]) {
if(tata[ant] && lim[ant][sf] > flux[ant][sf]) {
tata[sf] = ant;
int mi = INT_MAX;
int nod = sf;
while(nod != st) {
mi = min(mi, lim[tata[nod]][nod] - flux[tata[nod]][nod]);
nod = tata[nod];
}
if(mi < 0) continue;
rasp += mi;
nod = sf;
while(nod != st) {
flux[tata[nod]][nod] += mi;
flux[nod][tata[nod]] -= mi;
nod = tata[nod];
}
}
}
}
}
int main() {
fin >> n >> m;
while(m--) {
fin >> x >> y;
fin >> lim[x][y];
gr[x].push_back(y);
gr[y].push_back(x);
}
st = 1;
sf = n;
CautaFlux();
fout << rasp;
return 0;
}