Pagini recente » Cod sursa (job #704978) | Cod sursa (job #2937427) | Cod sursa (job #2344094) | Cod sursa (job #1798255) | Cod sursa (job #1083867)
//horatiu11
# include <cstdio>
# include <cstring>
# include <queue>
# include <vector>
# define nmax 1003
using namespace std;
int n,m,f,Ct[nmax][nmax],F[nmax][nmax],T[nmax],x,y,c,Min,sursa,dest;
bool viz[nmax];
queue <int>q;
vector <int>G[nmax];
inline bool bfs()
{
int x,i;
memset(viz,false,sizeof(viz));
q.push(sursa);
while(!q.empty())
{
x=q.front();q.pop();
for(i=0;i<G[x].size();++i)
if(Ct[x][G[x][i]]-F[x][G[x][i]]>0 && !viz[G[x][i]])
{
q.push(G[x][i]);viz[G[x][i]]=true;
T[G[x][i]]=x;
}
}
return viz[dest];
}
int main()
{
int i,j;
freopen("maxflow.in","r",stdin);
freopen("maxflow.out","w",stdout);
scanf("%d%d",&n,&m);
for(i=1;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&c);
Ct[x][y]=c;
G[x].push_back(y);
G[y].push_back(x);
}
sursa=1;dest=n;
while(bfs())
{
for(i=1;i<=n;++i)
if(Ct[i][n]-F[i][n]>0)
{
Min=Ct[i][n]-F[i][n];
for(j=i;j!=1;j=T[j])
if(Ct[T[j]][j]-F[T[j]][j]<Min)
Min=Ct[T[j]][j]-F[T[j]][j];
for(j=i;j!=1;j=T[j])
{
F[T[j]][j]+=Min;
F[j][T[j]]-=Min;
}
F[i][n]+=Min;
F[n][i]-=Min;
f+=Min;
}
}
printf("%d\n",f);
return 0;
}