Pagini recente » Cod sursa (job #1718404) | Cod sursa (job #1451698) | Cod sursa (job #1185446) | Cod sursa (job #3128317) | Cod sursa (job #2813246)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
#define max 1024
#define inf 200004
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int visited_flux[max];
int tata[max];
int capacitate[max][max];
int flux[max][max];
vector<int> la[max];
int n,m;
int flux_bfs(int source, int sink)
{
queue<int> coada;
for(int i=0;i<=n+1;i++)
{
visited_flux[i] = 0;
tata[i] = 0;
}
visited_flux[source]=1;
coada.push(source);
while(!coada.empty() && tata[sink]==0){
int cur = coada.front();
coada.pop();
for(auto vecin: la[cur])
{
if(capacitate[cur][vecin] - flux[cur][vecin]>0 && !visited_flux[vecin])
{
visited_flux[vecin]=1;
coada.push(vecin);
tata[vecin]=cur;
}
}
}
return (tata[sink]?true:false);
}
int fluxmax()
{
int rasp = 0;
while(flux_bfs(1,n)){
for(auto vecin:la[n])
{
if(flux[vecin][n]!=capacitate[vecin][n] && visited_flux[vecin])
{
tata[n]=vecin;
int fmin = inf;
int nod=n;
while(nod!=1)
{
if(capacitate[tata[nod]][nod]-flux[tata[nod]][nod]<fmin)
fmin = capacitate[tata[nod]][nod]-flux[tata[nod]][nod];
nod = tata[nod];
}
nod=n;
if(fmin!=0)
{
while(nod!=1)
{
flux[tata[nod]][nod] +=fmin;
flux[nod][tata[nod]] -=fmin;
nod = tata[nod];
}
rasp+=fmin;
}
}
}
}
return rasp;
}
int main()
{ int x,y,s;
f>>n>>m;
for(int i=1;i<m;i++)
{
f>>x>>y>>s;
capacitate[x][y] +=s;
la[x].push_back(y);
la[y].push_back(x);
}
g << fluxmax()<< endl;
return 0;
}