Pagini recente » Cod sursa (job #2448191) | Cod sursa (job #3124391) | Cod sursa (job #479515) | Cod sursa (job #1464063) | Cod sursa (job #1887416)
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
const int MAX_N = 1000;
FILE *fin = freopen("maxflow.in", "r", stdin);
FILE *fout = freopen("maxflow.out", "w", stdout);
int c[1 + MAX_N][1 + MAX_N];
int f[1 + MAX_N][1 + MAX_N];
int deUnde[1 + MAX_N];
std :: queue <int> Q;
std :: vector <int> vecini[1 + MAX_N];
//bitset <MAX_N> vis;
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();
std :: vector < int > :: iterator son;
for(son = vecini[node].begin(); son != vecini[node].end(); son++) {
if(c[node][*son] == f[node][*son] || deUnde[*son]) continue;
Q.push(*son);
deUnde[*son] = node;
}
}
return deUnde[D] != 0;
}
int main() {
int N, M, u, v, capacitate;
scanf("%d%d", &N, &M);
for (int i = 0; i < M; i++)
{
scanf("%d%d%d", &u, &v, &capacitate);
vecini[u].push_back(v);
vecini[v].push_back(u); // A nu se uita!!!!
c[u][v] = capacitate;
c[v][u] = 0;
}
for (int i = 1; i <= N; i++)
for (int j = 1; j <= N; j++)
f[i][j] = 0;
int S = 1;
int D = N;
int flux = 0;
while(bfs(S, D)) {
int fDrum = 0x7fffffff;
int u = D;
while(deUnde[u] != u) {
if (fDrum > c[deUnde[u]][u] - f[deUnde[u]][u]) {
fDrum = c[deUnde[u]][u] - f[deUnde[u]][u];
}
u = deUnde[u];
}
u = D;
while(deUnde[u] != u) {
f[deUnde[u]][u] += fDrum;
f[u][deUnde[u]] -= fDrum;
u = deUnde[u];
}
flux += fDrum;
}
printf("%d\n", flux);
return 0;
}