Pagini recente » Cod sursa (job #2332562) | Cod sursa (job #2277207) | Cod sursa (job #199768) | Istoria paginii utilizator/carriedaway | Cod sursa (job #914632)
Cod sursa(job #914632)
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<deque>
#define nmax 1000+5
using namespace std;
int din[nmax],mat[nmax][nmax],m,n,i,j,k,x,y,flux[nmax],rez;
vector<int>v[nmax];
bool viz[nmax];
deque<int>c;
int df(int x)
{
viz[x]=true;
c.push_back(x);
while (!c.empty())
{
x=c.front();
for (j=0;j<v[x].size();j++) if (mat[x][v[x][j]] && !viz[v[x][j]])
{
y=v[x][j];
viz[y]=true;
din[y]=x;
c.push_back(y);
if (!flux[x]) flux[y]=mat[x][y];else
flux[y]=min(flux[x],mat[x][y]);
}
c.pop_front();
}
return flux[n];
}
int main()
{
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,&k),mat[x][y]=k,v[x].push_back(y);
int ok=1;
while (df(1)>0)
{
rez+=flux[n];
x=n;
while (x)
{
mat[din[x]][x]-=flux[n];
x=din[x];
}
memset(din,0,sizeof(din));
memset(flux,0,sizeof(flux));
memset(viz,false,sizeof(viz));
}
printf("%d\n",rez);
return 0;
}