Pagini recente » Cod sursa (job #2218627) | Cod sursa (job #2231022) | Cod sursa (job #2796413) | Cod sursa (job #1218968) | Cod sursa (job #3337123)
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
constexpr int NMAX = 1005;
constexpr int INF = 1e7;
vector<vector<int>> lista;
int C[NMAX][NMAX], F[NMAX][NMAX];
vector<int>tata;
vector<bool> viz;
bool bfs(int s, int d) {
fill(viz.begin(), viz.end(), false);
fill(tata.begin(), tata.end(), -1);
queue<int> q;
q.push(s);
viz[s] = true;
tata[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == d) return true;
for (int v : lista[u]) {
if (!viz[v] && C[u][v] - F[u][v] > 0) {
q.push(v);
viz[v] = true;
tata[v] = u;
}
}
}
return false;
}
int main(){
int n,m;
fin>>n>>m;
lista.resize(n+1);
tata.resize(n+1);
viz.resize(n+1);
for (int i = 0; i<m;i++) {
int u,v,c;
fin>>u>>v>>c;
lista[u].push_back(v);
lista[v].push_back(u);
C[u][v] = c;
}
int maxfloww = 0;
while (bfs(1,n)) {
int pathflow = INF;
for (int v = n; v != 1; v=tata[v]) {
int u = tata[v];
pathflow = min(pathflow, C[u][v] - F[u][v]); // cred
}
for (int v = n; v!=1; v=tata[v]) {
int u = tata[v];
F[u][v] += pathflow;
F[v][u] -= pathflow;
}
maxfloww += pathflow;
}
fout<<maxfloww;
return 0;
}