Pagini recente » Cod sursa (job #2442192) | Cod sursa (job #1485448) | Cod sursa (job #2950645) | Cod sursa (job #744433) | Cod sursa (job #2679196)
#include <bits/stdc++.h>
using namespace std;
ifstream r("maxflow.in");
ofstream w("maxflow.out");
int n, m, parent[1002], rez[1002][1002];
vector<pair<int,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(make_pair(y, c));
rez[x][y]=c;
}
}
bool bfs(){
bool viz[1002];
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();
for(auto it: g[a]){
if(viz[it.first]==0 && rez[a][it.first]>0){
q.push(it.first);
parent[it.first]=a;
viz[it.first]=true;
}
}
}
return viz[n];
}
int flux(){
int flow=0;
while(bfs()!=0){
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;
}