Cod sursa(job #3350512)

Utilizator mtcmtcmtc mtc mtcmtc Data 8 aprilie 2026 20:50:16
Problema Flux maxim Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.4 kb
#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;
}