Pagini recente » Cod sursa (job #2180591) | Cod sursa (job #2049634) | Cod sursa (job #865502) | Cod sursa (job #1575409) | Cod sursa (job #2698742)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
int const N = 2001;
int v [N] , nxt [N] , vf [N] , sz;
int c [N][N] , e [N] , t [N];
ifstream f ("maxflow.in");
ofstream g ("maxflow.out");
void add (int a , int b){
vf [++ sz] = b;
nxt [sz] = v [a];
v [a] = sz;
}
int maxflow (int n){
queue <int> q;
vector <int> lst;
bool pp = true;
int ans = 0;
while (pp){
e [1] = (1 << 30);
fill (e + 2 , e + 1 + n , 0);
fill (t + 1 , t + 1 + n , 0);
lst . clear ();
q . push (1);
while (q . size ()){
int node = q . front ();
if (c [node][n])
lst . push_back (node);
for(int i = v [node] ; i ; i = nxt [i]){
int y = vf [i];
if (! c [node][y] || e [y] || y == n)
continue;
e [y] = min (e [node] , c [node][y]);
t [y] = node;
q . push (y);
}
q . pop ();
}
if (lst . empty ()){
pp = false;
continue;
}
int flow = 0;
for(auto x : lst){
int r = min (e [x] , c [x][n]);
flow += r;
c [x][n] -= r;
c [n][x] += r;
int node = x;
while (t [node]){
c [t [node]][node] -= r;
c [node][t [node]] += r;
node = t [node];
}
}
ans += flow;
}
return ans;
}
int main()
{
int n , m;
f >> n >> m;
for(int i = 1 ; i <= m ; ++ i){
int a , b , cost;
f >> a >> b >> cost;
add (a , b);
add (b , a);
c [a][b] = cost;
}
g << maxflow (n);
return 0;
}