Cod sursa(job #2415424)

Utilizator triscacezarTrisca Vicol Cezar triscacezar Data 25 aprilie 2019 23:23:47
Problema Flux maxim Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

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

int n,m,x,y,z,tata[1010],dist[1010],cap[1010][1010];
vector<int> v[1010];

bool Djikstra(){
    priority_queue<pair<int,int> > q;
    memset(dist,127,sizeof(dist));
    tata[n]=dist[1]=0;q.push({0,1});
    while(q.size()){
        int dst=-q.top().first;
        int poz= q.top().second;
        q.pop();
        if(dst!=dist[poz])continue;
        for(auto it:v[poz])
            if(dist[it]>dist[poz]+1&&cap[poz][it]){
                    dist[it]=dist[poz]+1;
                    tata[it]=poz;
                    q.push({-dist[it],it});
            }
    }
    return (tata[n]!=0);
}

int main()
{
    f>>n>>m;
    for(;m;m--){
        f>>x>>y>>z;
        v[x].push_back(y);
        v[y].push_back(x);
        cap[x][y]=z;
    }
    int Flx=0;
    for(;Djikstra();){
        int flxmax=1e9;
        for(int poz=n;poz!=1;poz=tata[poz])
            flxmax=min(flxmax,cap[tata[poz]][poz]);
        for(int poz=n;poz!=1;poz=tata[poz]){
            cap[tata[poz]][poz]-=flxmax;
            cap[poz][tata[poz]]+=flxmax;
        }
        Flx+=flxmax;
    }g<<Flx;
    return 0;
}