Cod sursa(job #2602139)

Utilizator dragos231456Neghina Dragos dragos231456 Data 15 aprilie 2020 22:40:25
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <bits/stdc++.h>
#define pb push_back
#define node adjacent[x][i]

using namespace std;

const int N=1005;

ifstream f("maxflow.in");
ofstream g("maxflow.out");

vector<int>adjacent[N];
int inf=(1<<30);
int flow[N][N],C[N][N],n,m,x,y,c,seen[N];
int a[N],k,mn,rez;
bool walk,increase_flow=true;

void dfs(int x,int q,int fl)
{
    seen[x]=1;
    a[q]=x;
    if(x==n)
    {
        walk=1;
        mn=fl;
        k=q;
        return;
    }
    for(int i=0;i<adjacent[x].size() && !walk;++i)
    {
        //cout<<x<<' '<<node<<' '<<C[x][node]<<' '<<flow[x][node]<<endl;
        if(!seen[node] && (C[x][node]-flow[x][node]>0)) dfs(node,q+1,min(fl,C[x][node]-flow[x][node]));
    }
}

int main()
{
    f>>n>>m;
    for(int i=1;i<=m;++i)
    {
        f>>x>>y>>c;
        C[x][y]=c;
        adjacent[x].pb(y);
        adjacent[y].pb(x);
    }
    while(increase_flow)
    {
        increase_flow=0; walk=0;
        for(int i=1;i<=n;++i) seen[i]=0;
        dfs(1,1,inf);
        if(walk)
        {
            increase_flow=1; rez+=mn;
            //cout<<a[1]<<' ';
            for(int i=2;i<=k;++i)
            {
                //cout<<a[i]<<' ';
                flow[a[i-1]][a[i]]+=mn;
                flow[a[i]][a[i-1]]-=mn;
            }
            //cout<<endl;
        }
    }
    g<<rez;
    return 0;
}