Cod sursa(job #2209046)

Utilizator DovlecelBostan Andrei Dovlecel Data 1 iunie 2018 16:30:36
Problema Flux maxim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream in("maxflow.in");
ofstream out("maxflow.out");
const int N=1000;
const long long INF=550000000;
int n,m,q[N],s,d,pred[N],f[N][N],c[N][N];
bool viz[N],a[N][N];
void reset_viz()
{
    for(int i=1;i<=n;i++)
        viz[i]=false;
}
bool bfs()
{
    int st=0,dr=-1,x;
    reset_viz();
    q[++dr]=s;
    while(st<=dr)
    {
        x=q[st++];
        for(int y=1;y<=n;y++)
        {
            if(!viz[y] && c[x][y]>f[x][y])
            {
                q[++dr]=y;
                viz[y]=true;
                pred[y]=x;
                if(y==d)
                    return true;
            }
        }
    }
    return false;
}
int min_drum()
{
    int x=d,vmin=INF;
    while(x!=s)
    {
        vmin=min(vmin,c[pred[x]][x]-f[pred[x]][x]);
        x=pred[x];
    }
    return vmin;
}
void actualizare_drum(int vmin)
{
    int x=d;
    while(x!=s)
    {
        f[pred[x]][x]+=vmin;
        f[x][pred[x]]-=vmin;
        x = pred[x];
    }
}
int main()
{
    int x,y,cap,vmin,flux=0;
    in>>n>>m;
    s=1;
    d=n;
    for(int i=1;i<=m;i++)
    {
        in>>x>>y>>cap;
        a[x][y]=true;
        c[x][y]=cap;
    }
    while(bfs())
    {
        vmin=min_drum();
        flux+=vmin;
        actualizare_drum(vmin);
    }
    out<<flux;
    return 0;
}