Pagini recente » Cod sursa (job #1651508) | Cod sursa (job #2701073) | Cod sursa (job #1003623) | Cod sursa (job #1959) | Cod sursa (job #2679227)
#include <bits/stdc++.h>
using namespace std;
ifstream r("maxflow.in");
ofstream w("maxflow.out");
int n, m, parent[1002], rez[1002][1002];
bool viz[1002];
vector<int>g[1002];
void read()
{
r>>n>>m;
for(int i=0; i<m; i++)
{
int x, y, c;
r>>x>>y>>c;
g[x].push_back(y);
g[y].push_back(x);
rez[x][y]=c;
}
}
bool bfs()
{
memset(viz, 0, sizeof(viz));
queue<int>q;
q.push(1);
viz[1]=1;
parent[1]=-1;
while(q.size()!=0)
{
int a=q.front();
q.pop();
if(a!=n)
{
for(auto it: g[a])
{
if(viz[it]==0 && rez[a][it]>0)
{
q.push(it);
parent[it]=a;
viz[it]=true;
}
}
}
}
return viz[n];
}
int flux()
{
int flow=0;
while(bfs()!=0)
{
for(auto it: g[n])
{
if(rez[it][n]>0 && viz[it]==1)
{
parent[n]=it;
int fluxm=1000000000, nod=n;
while(nod!=1)
{
fluxm=min(fluxm, rez[parent[nod]][nod]);
nod=parent[nod];
}
nod=n;
while(nod!=1)
{
rez[parent[nod]][nod]-=fluxm;
rez[nod][parent[nod]]+=fluxm;
nod=parent[nod];
}
flow+=fluxm;
}
}
}
return flow;
}
int main()
{
read();
w<<flux();
return 0;
}