Pagini recente » Cod sursa (job #1635459) | Cod sursa (job #1839289) | Cod sursa (job #2902012) | Cod sursa (job #90147) | Cod sursa (job #2293229)
#include <bits/stdc++.h>
using namespace std;
int n,m,x,y,c,ans;
vector<int>nod[1005],where;
int cap[1005][1005],last[1005],flo[1005][1005];
bool bfs()
{
queue<int>q;
q.push(1);
memset(last,-1,sizeof last);
last[1]=0;
while(!q.empty())
{
int pos=q.front();
q.pop();
for(int j=0;j<nod[pos].size();j++)
{
int i=nod[pos][j];
if(last[i]!=-1 || cap[pos][i]<=flo[pos][i])
continue;
last[i]=pos;
q.push(i);
}
}
return last[n]!=-1;
}
int main()
{
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
fin>>n>>m;
for(int i=1;i<=m;i++)
{
fin>>x>>y>>c;
cap[x][y]=c;
nod[x].push_back(y);
if(y==n)
where.push_back(x);
}
while(bfs())
{
for(int i=1;i<=n;i++)
{
if(cap[i][n]<=flo[i][n])
continue;
c=cap[i][n]-flo[i][n];
int pos=i;
while(last[pos]!=0)
{
c=min(c,cap[last[pos]][pos]-flo[last[pos]][pos]);
pos=last[pos];
}
pos=i;
ans+=c;
flo[pos][n]+=c;
flo[n][pos]-=c;
while(last[pos]!=0)
{
flo[last[pos]][pos]+=c;
flo[pos][last[pos]]-=c;
pos=last[pos];
}
}
}
fout<<ans;
return 0;
}