Pagini recente » Cod sursa (job #73997) | Cod sursa (job #2088688) | Cod sursa (job #1289455) | Cod sursa (job #2689636) | Cod sursa (job #2961151)
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n, m, x, y, z, s, t;
int capacitate[5001][5001];
vector<vector<int>> graf;
int tata[5001];
bool bfs(){
queue<int> q;
int vizitat[n+1];
for(int i=1 ; i<=n; i++)
vizitat[i] = 0;
q.push(s);
vizitat[s] = 1;
tata[s] = -1;
while(!q.empty()){
int u = q.front();
q.pop();
for(auto v: graf[u]){
if(vizitat[v] == 0 && capacitate[u][v] != 0){
tata[v] = u;
if(v == t)
return true;
vizitat[v] = 1;
q.push(v);
}
}
}
return false;
}
int flux_maxim(){
int fluxmax = 0;
while (bfs()){
int u, v = t, flux = INT_MAX;
while(v!=s){
u = tata[v];
if(capacitate[u][v] < flux)
flux = capacitate[u][v];
v = tata[v];
}
v = t;
while(v != s){
u = tata[v];
capacitate[u][v] -= flux;
capacitate[v][u] += flux;
v = tata[v];
}
fluxmax += flux;
}
return fluxmax;
}
int main(){
fin>>n>>m;
graf.resize(n+1);
for(int i=1; i<= m; i++){
fin>>x>>y>>z;
graf[x].push_back(y);
graf[y].push_back(x);
capacitate[x][y] = z;
}
s = 1;
t = n;
fout<<flux_maxim();
}