Pagini recente » Cod sursa (job #622219) | Cod sursa (job #189604) | Cod sursa (job #1449586) | Cod sursa (job #943291) | Cod sursa (job #2564655)
#include<bits/stdc++.h>
using namespace std;
#define NMAX 1005
#define INF (1<<30)-1
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n,m;
int cap[NMAX][NMAX];
vector<int> ma[NMAX];
void citire()
{
//cout<<1;
fin>>n>>m;
for(int i=1;i<=m;i++)
{
int x,y,c;
fin>>x>>y>>c;
ma[x].push_back(y);
cap[x][y] = c;
}
}
int bfs(int s,int t,vector<int>& parent)
{
fill(parent.begin(),parent.end(),-1);
parent[s] = -2;
queue<pair<int,int> > q;
q.push({s,INF});
while(!q.empty())
{
int curent = q.front().first;
int flow = q.front().second;
q.pop();
for(int i=0;i<ma[curent].size();i++)
{
int vecin = ma[curent][i];
if(parent[vecin] == -1 && cap[curent][vecin])
{
parent[vecin] = curent;
int new_flow = min(flow,cap[curent][vecin]);
if(vecin == t)
return new_flow;
else
q.push({vecin,new_flow});
}
}
}
return 0;
}
int max_flow(int s,int t)
{
int new_flow = 0;
vector<int> parent(n+1);
int flow = 0;
while(new_flow = bfs(1,n,parent))
{
flow += new_flow;
int cur = t;
while(cur!=s)
{
int prev = parent[cur];
cap[prev][cur] -= new_flow;
cap[cur][prev] += new_flow;
cur = prev;
}
}
return flow;
}
int main()
{
citire();
fout<<max_flow(1,n);
}