Pagini recente » Cod sursa (job #2327576) | Cod sursa (job #2259470) | Cod sursa (job #2957405) | Cod sursa (job #2732621) | Cod sursa (job #3226216)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("maxflow.in");
ofstream fout ("maxflow.out");
int capacitate[1005][1005];
vector <int> g[1005];
int n, m;
bool viz[1005];
bool return_dfs=false;
int dfs (int nod, int minflow)
{
viz[nod]=1;
if (nod==n) {
return_dfs=true;
return minflow;
}
for (int vecin : g[nod]) {
if (return_dfs==true) return minflow;
if (capacitate[nod][vecin]!=0&&viz[vecin]==0) {
minflow=min(minflow, capacitate[nod][vecin]);
//cout<<nod<<"--->"<<vecin<<" "<<minflow<<endl;
minflow=dfs(vecin, minflow);
capacitate[nod][vecin]-=minflow;
capacitate[vecin][nod]+=minflow;
}
}
return minflow;
}
int maxFlow()
{
int flux=0;
while (true) {
int minflow=dfs(1, INT_MAX);
//cout<<"----------"<<endl;
//cout<<minflow<<endl;
for (int i=1; i<=n; i++) viz[i]=0;
if (minflow==INT_MAX) break;
flux+=minflow;
return_dfs=false;
}
return flux;
}
int main()
{
fin.tie(0); fin.sync_with_stdio(false);
int a, b, d; fin>>n>>m;
for (int i=1; i<=m; i++) {
fin>>a>>b>>d;
g[a].push_back(b);
g[b].push_back(a); //Muchiile reziduale;
capacitate[a][b]=d;
}
int flow=maxFlow();
fout<<flow;
return 0;
}