Pagini recente » Cod sursa (job #3176775) | Cod sursa (job #1588814) | Cod sursa (job #2277137) | Cod sursa (job #604320) | Cod sursa (job #2778239)
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
string prob="maxflow";
ifstream in(prob+".in");
ofstream out(prob+".out");
int muchii[1001][1001];
int vecini[1001][1001];
int marime[1001];
bool viz[1001];
int c=110010;
int n,m;
int dfs(int nod,int minn){
if(viz[nod])return 0;
viz[nod]=1;
if(nod==n)return minn;
for(int j=0;j<marime[nod];j++){
int i=vecini[nod][j];
if(muchii[nod][i]>=c){
int tmp=dfs(i,min(minn,muchii[nod][i]));
if(tmp){
muchii[nod][i]-=tmp;
muchii[i][nod]+=tmp;
return tmp;
}
}
}
return 0;
}
int main(){
in>>n>>m;
int x,y,z;
for(int i=0;i<m;i++){
in>>x>>y>>z;
vecini[x][marime[x]++]=y;
vecini[y][marime[y]++]=x;
muchii[x][y]=max(muchii[x][y],z);
muchii[y][x]=max(muchii[y][x],0);
}
int s=0;
while(c){
memset(viz,0,n+1);
int tmp=dfs(1,110010);
s+=tmp;
if(!tmp){
c>>=1;
}
}
out<<s;
}