Pagini recente » Cod sursa (job #2743747) | Cod sursa (job #2934848) | Cod sursa (job #2232142) | Cod sursa (job #2648205) | Cod sursa (job #2655134)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
const int nmax = 1005;
int n, m, c[nmax][nmax], f[nmax][nmax], t[nmax];
bool viz[nmax];
vector <int> G[nmax];
bool dfs(){
memset(viz, 0, sizeof viz);
queue <int> coada;
coada.push(1);
viz[1] = true;
while (!coada.empty()){
int nod = coada.front();
coada.pop();
for (auto it : G[nod]){
if (viz[it] == true || f[nod][it] == c[nod][it]) continue;
coada.push(it);
viz[it] = true;
t[it] = nod;
if (it == n){
return true;
}
}
}
return false;
}
int main(){
fin >> n >> m;
for (int i = 1; i <= m; ++i){
int x, y, z;
fin >> x >> y >> z;
G[x].push_back(y);
G[y].push_back(x);
c[x][y] += z;
}
int flow, fmin;
for (flow = 0; dfs(); flow += fmin){
fmin = 1e9;
for (int nod = n; nod != 1; nod = t[nod]){
fmin = min(fmin, c[t[nod]][nod] - f[t[nod]][nod]);
}
for (int nod = n; nod != 1; nod = t[nod]){
f[t[nod]][nod] += fmin;
f[nod][t[nod]] -= fmin;
}
}
fout << flow << "\n";
fin.close();
fout.close();
return 0;
}