Pagini recente » Cod sursa (job #1989720) | Cod sursa (job #1101304) | Cod sursa (job #1121100) | Cod sursa (job #2623811) | Cod sursa (job #2192963)
#include <bits/stdc++.h>
#define pii pair<int,int>
#define Mmax 5005
#define Nmax 1002
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int n,m,S = 1,D,x,y,c,ans;
vector<pii> E;
vector<int> v[Nmax],G[Nmax];
vector<int> H;
int uz[Nmax];
bool bfs()
{
memset(uz,0,sizeof(uz));
H.clear();
H.push_back(S);
for (int i=1; i<=n; i++) G[i].clear();
for (int i=0; i<H.size(); i++)
{
for (auto it : v[H[i]])
{
if (!E[it].second) continue;
if (uz[E[it].first]==0)
{
uz[E[it].first] = uz[H[i]]+1;
H.push_back(E[it].first);
}
if (uz[E[it].first]==uz[H[i]]+1) G[H[i]].push_back(it);
if (E[it].first==D) return 1;
}
}
return 0;
}
int dfs(int nod,int ant,int flow)
{
int crtF = 0;
if (nod==D) return flow;
for (auto it : G[nod])
{
if (E[it].first==ant) continue;
int aux = dfs(E[it].first,nod,min(flow-crtF,E[it].second));
crtF += aux;
E[it].second -= aux;
E[it^1].second += aux;
}
return crtF;
}
int main()
{
f>>n>>m;
D = n;
for (int i=1; i<=m; i++)
{
f>>x>>y>>c;
v[x].push_back(E.size());
E.push_back({y,c});
v[y].push_back(E.size());
E.push_back({x,0});
}
while (bfs())
{
ans += dfs(S,-1,1e9);
}
g<<ans;
return 0;
}