Pagini recente » Cod sursa (job #2534440) | Cod sursa (job #651189) | Cod sursa (job #601145) | Cod sursa (job #3300896) | Cod sursa (job #3337129)
#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
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];
int tata[NMAX];
bool bfs(int s, int d) {
memset(tata, -1, sizeof(tata));
queue<int> q;
q.push(s);
tata[s] = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
if (u == d) return true;
for (int v : lista[u]) {
if (tata[v]==-1 && C[u][v] - F[u][v] > 0) {
q.push(v);
tata[v] = u;
}
}
}
return false;
}
int main(){
int n,m;
fin>>n>>m;
lista.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;
}