Pagini recente » Cod sursa (job #1003629) | Cod sursa (job #3323998) | Cod sursa (job #2079004) | Monitorul de evaluare | Cod sursa (job #3356859)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("maxflow.in");
ofstream fout("maxflow.out");
int n,m;
vector<int> v[1003];
int cost[1003][1003];
int viz[1003];
int dfs(int nod,int bottleneck)
{
if (nod==n) return bottleneck;
viz[nod]=1;
for (auto i:v[nod])
{
if (!viz[i] && cost[nod][i]>0)
{
int val=dfs(i,min(bottleneck,cost[nod][i]));
if (val>0)
{
cost[nod][i]-=val;
cost[i][nod]+=val;
return val;
}
}
}
return 0;
}
int rez;
int main()
{
fin>>n>>m;
while (m--)
{
int x,y,c;
fin>>x>>y>>c;
if (cost[x][y]==0 && cost[y][x]==0)
{
v[x].push_back(y);
v[y].push_back(x);
}
cost[x][y]+=c;
}
int aux=dfs(1,1e9);
while (aux!=0)
{
rez+=aux;
memset(viz,0,sizeof(viz));
aux=dfs(1,1e9);
}
fout<<rez;
return 0;
}