Pagini recente » Cod sursa (job #2546045) | Cod sursa (job #1152216) | Cod sursa (job #139534) | Cod sursa (job #2439279) | Cod sursa (job #2415441)
#include <bits/stdc++.h>
using namespace std;
ifstream f("maxflow.in");
ofstream g("maxflow.out");
int n,m,x,y,z,tata[1010],cap[1010][1010];
vector<int> v[1010];
bool Djikstra(){
queue<int> q;
memset(tata,0,sizeof(tata));
q.push(1);
while(q.size()){
int poz=q.front();
if(poz==n)break;
q.pop();
for(auto it:v[poz])
if((!tata[it])&&cap[poz][it]){
tata[it]=poz;
q.push(it);
}
}
return (tata[n]!=0);
}
int main()
{
f>>n>>m;
for(;m;m--){
f>>x>>y>>z;
v[x].push_back(y);
v[y].push_back(x);
cap[x][y]=z;
}
int Flx=0;
for(;Djikstra();){
for(auto it:v[n])
if(cap[it][n]){
int flxmax=cap[it][n];
for(int poz=it;poz!=1&&flxmax!=0;poz=tata[poz])
flxmax=min(flxmax,cap[tata[poz]][poz]);
if(!flxmax)continue;
for(int poz=it;poz!=1;poz=tata[poz]){
cap[tata[poz]][poz]-=flxmax;
cap[poz][tata[poz]]+=flxmax;
}
Flx+=flxmax;
}
}g<<Flx;
return 0;
}