Pagini recente » Cod sursa (job #2349212) | Monitorul de evaluare | Cod sursa (job #3160639) | Cod sursa (job #1128394) | Cod sursa (job #3350511)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("maxflow.in");
ofstream cout("maxflow.out");
vector<int>adj[1005];
int c[1005][1005];
int f[1005][1005];
int l[1005];
int n;
int bfs(){
for(int i=1;i<=n;i++){
l[i]=-1;
}
queue<int>q;
q.push(1);
l[1]=0;
while(!q.empty()){
int x=q.front();
q.pop();
for(auto y:adj[x]){
int s=c[x][y]-f[x][y];
if(s>0&&l[y]==-1){
l[y]=l[x]+1;
q.push(y);
if(y==n) return 1;
}
}
}
return 0;
}
int dfs(int x,int flow){
if(x==n) return flow;
for(auto y:adj[x]){
if(l[y]==l[x]+1&&c[x][y]-f[x][y]>0){
int f1=min(flow,c[x][y]-f[x][y]);
int flw=dfs(y,f1);
if(flw>0){
f[x][y]+=flw;
f[y][x]-=flw;
}
return flw;
}
}
return 0;
}
int main()
{
int m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y,z;
cin>>x>>y>>z;
adj[x].push_back(y);
adj[y].push_back(x);
c[x][y]=z;
}
while(bfs()){
int flow=1e9;
while((flow=dfs(1,1e9))>0){
;
}
}
int mx=0;
for(auto e:adj[1]){
mx+=f[1][e];
}
cout<<mx;
return 0;
}