Pagini recente » Borderou de evaluare (job #1020753) | Borderou de evaluare (job #2685714) | Borderou de evaluare (job #2685718) | Cod sursa (job #3161222) | Cod sursa (job #2111988)
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
using namespace std;
FILE *fin = freopen("maxflow.in", "r", stdin);
FILE *fout = freopen("maxflow.out", "w", stdout);
const int MAX_N = 1000;
vector < int > neighbor[1 + MAX_N];
int c[1 + MAX_N][1 + MAX_N];
int deUnde[1 + MAX_N];
queue < int > Q;
vector < int > inD;
bool BFS(int S, int D){
memset(deUnde, 0, sizeof(deUnde));
Q.push(S);
deUnde[S] = S;
while (!Q.empty()){
int node = Q.front();
Q.pop();
vector < int > :: iterator it;
for (it = neighbor[node].begin(); it != neighbor[node].end(); it ++){
if (c[node][*it] == 0 || deUnde[*it]) continue;
if (*it != D)
Q.push(*it);
deUnde[*it] = node;
}
}
return deUnde[D] != 0;
}
int main()
{
int N, M, u, v, capacity, flux = 0;
scanf("%d %d", &N, &M);
for (int i = 1; i <= M; i++)
{
scanf("%d %d %d", &u, &v, &capacity);
neighbor[u].push_back(v);
neighbor[v].push_back(u);
c[u][v] = capacity;
}
int S = 1, D = N;
for (int u = 1; u <= N; u++)
if (c[u][D] > 0)
inD.push_back(u);
while (BFS(S, D)){
vector < int > :: iterator it;
for (it = inD.begin(); it != inD.end(); it ++){
if (deUnde[*it] != 0){
deUnde[D] = *it;
int fDrum = 0x7fffffff;
int u = D;
while ( u != S ){
if (fDrum > c[deUnde[u]][u])
fDrum = c[deUnde[u]][u];
u = deUnde[u];
}
u = D;
while ( u != S){
c[deUnde[u]][u] -= fDrum;
c[u][deUnde[u]] += fDrum;
u = deUnde[u];
}
flux += fDrum;
}
}
}
printf("%d\n", flux);
}