Pagini recente » Cod sursa (job #2083322) | Cod sursa (job #3320037) | Cod sursa (job #3343460) | Cod sursa (job #3340064) | Cod sursa (job #3356868)
#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 tata[1003];
struct da
{
int nod;
int bottleneck;
};
int Maxval=1e9;
int bfs(int start)
{
queue<da> q;
q.push({1,Maxval});
viz[start]=1;
while(!q.empty())
{
int nod=q.front().nod;
int bottleneck=q.front().bottleneck;
q.pop();
if (nod==n) return bottleneck;
for (auto i:v[nod])
{
if (!viz[i] && cost[nod][i]>0)
{
tata[i]=nod;
viz[i]=1;
q.push({i,min(bottleneck,cost[nod][i])});
}
}
}
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=bfs(1);
while (aux!=0)
{
rez+=aux;
int root=n;
while (tata[root]!=root)
{
int ant=tata[root];
cost[ant][root]-=aux;
cost[root][ant]+=aux;
root=tata[root];
}
memset(viz,0,sizeof(viz));
aux=bfs(1);
}
fout<<rez;
return 0;
}