Pagini recente » Cod sursa (job #1603960) | Cod sursa (job #2749232) | Cod sursa (job #2514569) | Cod sursa (job #3175917) | Cod sursa (job #2095536)
#include<bits/stdc++.h>
#define Dim 1<<28
#define N 1005
using namespace std;
vector <int> G[N];
queue <int> q;
int n,m,C[N][N],pred[N],f[N][N];
bool viz[N];
void read()
{
int i,x,y,z;
freopen("maxflow.in","r",stdin);
scanf("%d%d",&n,&m);
for(i=1;i<=m;i++)
{
scanf("%d %d %d ",&x,&y,&z);
G[x].push_back(y);
G[y].push_back(x);
C[x][y]=z;
}
}
bool bfs()
{
int i,j,nod,nod2;
memset(viz,0,sizeof(viz));
q.push(1);
viz[1]=1;
while(!q.empty())
{
nod=q.front();
q.pop();
if(nod==n) continue;
for(j=0;j<G[nod].size();j++)
{
nod2=G[nod][j];
if(viz[nod2] || f[nod][nod2]==C[nod][nod2]) continue;
viz[nod2]=1;
q.push(nod2);
pred[nod2]=nod;
}
}
return viz[n];
}
int ford_fulkerson()
{
int flow=0,i,f_min=Dim,nod;
while(bfs())
{
nod=n;
while(nod!=1)
{
f_min=min(f_min,C[pred[nod]][nod]-f[pred[nod]][nod]);
nod=pred[nod];
}
nod=n;
while(nod!=1)
{
f[pred[nod]][nod]+=f_min;
f[nod][pred[nod]]-=f_min;
nod=pred[nod];
}
flow+=f_min;
}
return flow;
}
int main()
{
read();
freopen("maxflow.out","w",stdout);
//cout<<bfs();
cout<<ford_fulkerson();
}