Pagini recente » Atasamentele paginii Pante | Cod sursa (job #3356657) | Cod sursa (job #3356658)
#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];
int rez;
int timp=1;
struct da
{
int nod;
int bottleneck;
};
int Maxval=1e9;
int bfs(int start)
{
queue<da> q;
q.push({start,Maxval});
viz[start]=timp;
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]!=timp && cost[nod][i]>0)
{
viz[i]=timp;
tata[i]=nod;
q.push({i,min(bottleneck,cost[nod][i])});
}
}
}
return 0;
}
int main()
{
fin>>n>>m;
while (m--)
{
int x,y,c;
fin>>x>>y>>c;
if (cost[x][y]==cost[y][x] && cost[x][y]==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 (root!=1)
{
int ant=tata[root];
cost[ant][root]-=aux;
cost[root][ant]+=aux;
root=ant;
}
timp++;
aux=bfs(1);
}
fout<<rez;
return 0;
}