Pagini recente » Cod sursa (job #1422689) | Cod sursa (job #269722) | Cod sursa (job #2264962) | Cod sursa (job #897915) | Cod sursa (job #1144537)
#include <fstream>
#include <queue>
#include <vector>
#define INF 999999999
#define NMAX 1005
using namespace std;
FILE* f=freopen("maxflow.in","r",stdin);
FILE* o=freopen("maxflow.out","w",stdout);
int n,m;
int flow;
int father[NMAX];
int cap[NMAX][NMAX],flw[NMAX][NMAX];
vector<int> graph[NMAX];
inline void INIT(int a[],int v, int n)
{
for(int i=0;i<=n;++i) a[i]=v;
}
inline int minim(int a, int b) { return (a<b)?a:b; }
int MoreFlow()
{
queue<int> q;
INIT(father,0,n);
q.push(1);
father[1]=-1;
int x;
while(!q.empty())
{
x=q.front();
q.pop();
if(x==n) continue;
for(int i=0;i<graph[x].size();++i)
{
int ind=graph[x][i];
if(cap[x][ind]==flw[x][ind]||father[ind]) continue;
father[ind]=x;
q.push(ind);
}
}
return father[n];
}
void MaxFlow()
{
for(flow=0;MoreFlow();)
{
for(int i=0;i<graph[n].size();++i)
{
int where=graph[n][i];
if(cap[where][n]==flw[where][n]||!father[where]) continue;
father[n]=where;
int minflow=INF;
for(where=n;where!=1;where=father[where])
{
int ft=father[where];
minflow=minim(minflow,cap[ft][where]-flw[ft][where]);
if(ft==0) break;
}
if(minflow>0)
{
for(where=n;where!=1;where=father[where])
{
int ft=father[where];
flw[ft][where]+=minflow;
flw[where][ft]-=minflow;
}
}
flow+=minflow;
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<m;++i)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
cap[x][y]=+c;
graph[x].push_back(y);
graph[y].push_back(x);
}
MaxFlow();
printf("%d",flow);
return 0;
}