Pagini recente » Cod sursa (job #440584) | Cod sursa (job #399360) | Cod sursa (job #1868785) | Cod sursa (job #1215822) | Cod sursa (job #3235948)
#include <fstream>
#include <vector>
#include <cstdint>
#include <queue>
#include <memory.h>
#include <iostream>
const int NMAX = 1001;
std::ifstream in("maxflow.in");
std::ofstream out("maxflow.out");
class MaxFlow{
public:
MaxFlow(int n) : n(n){}
uint32_t getFlow(){
uint32_t flow = 0;
while(bfs(1,n)){
int minf = 1e7;
int aux = n;
while(parent[aux] != -1){
minf = std::min(c[parent[aux]][aux]-f[parent[aux]][aux],minf);
aux=parent[aux];
}
aux = n;
while(parent[aux] != -1){
f[parent[aux]][aux] += minf;
f[aux][parent[aux]] -= minf;
aux=parent[aux];
}
flow+=minf;
}
return flow;
}
void add_edge(int a,int b,int cost){
c[a][b] = cost;
}
private:
int n;
int32_t f[NMAX][NMAX];
int32_t c[NMAX][NMAX];
std::vector<int> g[NMAX];
int parent[NMAX];
bool bfs(int start,int finish){
std::queue<int>q;
q.push(start);
memset(parent,0,NMAX*sizeof(int));
parent[start] = -1;
while(!q.empty()){
int aux = q.front();
q.pop();
for(int x=1;x<=n;x++){
if(parent[x] == 0 && c[aux][x] - f[aux][x] > 0){
parent[x] = aux;
q.push(x);
if(x == finish){
return 1;
}
}
}
}
return 0;
}
};
int main(){
int n,m;
in>>n>>m;
MaxFlow prob(n);
while(m--){
int a,b,c;
in>>a>>b>>c;
prob.add_edge(a,b,c);
}
std::cout<<"Here\n";
out<<prob.getFlow();
return 0;
}