Pagini recente » Cod sursa (job #1140765) | Cod sursa (job #2611330) | Cod sursa (job #996726) | Cod sursa (job #712561) | Cod sursa (job #2411321)
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
const string file = "maxflow";
const ll INF = 9223372036854775807ll;
const int inf = 2147483647, nmax = 1005;
int n, m, c[nmax][nmax], f[nmax][nmax], pred[nmax];
vector<int> v[nmax];
bool ok[nmax];
bool bfs()
{
memset(ok, 0, sizeof(ok));
queue<int> q;
q.push(1);
while(!q.empty()){
int nod = q.front();
q.pop();
for (auto x : v[nod])
if(!ok[x] && c[nod][x] > f[nod][x]){
ok[x] = 1;
pred[x] = nod;
q.push(x);
if(x == n)
return 1;
}
}
return 0;
}
int main()
{
ifstream fin (file+".in");
ofstream fout (file+".out");
fin >> n >> m;
for (int i = 1; i <= m; ++i){
int x, y, C;
fin >> x >> y >> C;
v[x].push_back(y);
v[y].push_back(x);
c[x][y] = C;
}
int maxflow = 0;
while(bfs())
for (auto nod : v[n]){
pred[n] = nod;
int mn = inf;
int I = n;
while(I != 1){
mn = min(mn, c[pred[I]][I]-f[pred[I]][I]);
I = pred[I];
}
maxflow += mn;
I = n;
while(I != 1){
f[pred[I]][I] += mn;
f[I][pred[I]] -= mn;
I = pred[I];
}
}
fout << maxflow << "\n";
return 0;
}