Pagini recente » Diferente pentru aib intre reviziile 17 si 26 | Cod sursa (job #1172137)
#include <fstream>
#include <vector>
#include <queue>
#include <string.h>
#define max 1001
#define infinity 1000000
using namespace std;
/* muchiile din graf */
vector < vector <int> > graph;
/* capacitatile muchiilor */
int C[max][max];
/* fluxul actual pe muchie */
int F[max][max];
/* vector de parinti
* folosit la bfs pt actualizarea capacitatilor reziduale */
int parents[max];
int path[max];
int N, M;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int bfs(int source, int target){
/* clear parents vector */
memset(parents, -1, sizeof(parents));
/* clear current path */
memset(path, 0, sizeof(path));
queue<int> q;
parents[source] = -2;
path[source] = infinity;
q.push(source);
while(!q.empty()){
int elem = q.front();
q.pop();
for(int i = 0; i < graph[elem].size(); i++){
int u = graph[elem][i];
if(C[elem][u] - F[elem][u] > 0 && parents[u] == -1){
parents[u] = elem;
path[u] = min(path[elem], C[elem][u] - F[elem][u]);
if(u == target) return path[u];
q.push(u);
}
}
}
return 0;
}
int edmonds_karp(int source, int target){
int rezid, max_flow = 0, back;
while(true){
rezid = bfs(source, target);
if(rezid == 0) break;
back = target;
while(source != back){
F[parents[back]][back] += rezid;
F[back][parents[back]] -= rezid;
back = parents[back];
}
max_flow += rezid;
}
return max_flow;
}
int main(){
int a, b, c;
vector<int> v;
fin >> N >> M;
for(int i = 0; i < N; i++)
graph.push_back(v);
for(int i = 0; i < M; i++){
fin >> a >> b >> c;
graph[--a].push_back(--b);
graph[b].push_back(a);
C[a][b] = c;
}
int max_flow = edmonds_karp(0,N-1);
fout << max_flow << "\n";
return 0;
}