Cod sursa(job #3272310)

Utilizator PescaPescariu Matei Alexandru Pesca Data 29 ianuarie 2025 09:18:33
Problema Flux maxim Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.75 kb
#include <bits/stdc++.h>
namespace flux{
    #ifdef fakeFILES
    std::ifstream fin("input.in");
    std::ofstream fout("input.out");
    #else
    std::ifstream fin("maxflow.in");
    std::ofstream fout("maxflow.out");
    #endif
    using namespace std;
    int n,fluxRez;
    int const N = 1025, INF = 1e9;
    vector<int> g[N];
    int cost[N][N], flux[N][N];

    inline int liber(int nod1,int nod2){
        return cost[nod1][nod2] - flux[nod1][nod2];
    }
    vector<int> bfs(int start, int destination){
        queue <int>q;
        q.push(start);
        int d[N];
        for(int i = 1; i <=n; i++)
            d[i] = 0;
        while(q.size())
        {
            int node=q.front();
            vector <int>::iterator it;
            for(auto it=g[node].begin();it!=g[node].end();it++)
            {
                if(d[*it] || flux[node][*it] == cost[node][*it])
                    continue;
                d[*it]=node;

                if(*it == destination){
                    vector<int> rez;
                    while(destination){
                       rez.push_back(destination);
                       destination = d[destination];
                    }
                    reverse(rez.begin(),rez.end());
                    return rez;
                }
                q.push(*it);

            }
            q.pop();
        }
        return vector<int>();

    }

    void maxFlow(int source, int destination){
        vector<int> path;
        path = bfs(source,destination);
        while(!path.empty()){
            int mx = 0;
            for(size_t i = 1; i < path.size(); i++){
                mx = max(mx, liber(path[i-1],path[i]));
            }
            fluxRez += mx;
            for(size_t i = 1; i < path.size(); i++){
                flux[path[i-1]][path[i]] += mx;
                cost[path[i]][path[i-1]] += mx;
            }
            path = bfs(source,destination);
        }
        fout <<fluxRez;
    }

    void run(){
        cout << "WHAT";
        return;
        int m;
        fin >> n >> m;
        cout << n << " a " << endl ;

        while(m--){
            int x,y,c;
            fin >> x >> y >> c;



            cost[x][y] += c;
            g[x].push_back(y);
            g[y].push_back(x); /// y->x edge is the reverse edge which has capacity 0 for now.

        }
        /// 1 is source, n is destination
        ///maxFlow(1,n);
    }
}
/*******************
MOMENTAN LIPSESC:
    - muchii / noduri critice.
    - componente tare conexe.
    - APM kruskal / prim.
    - Belman Ford.
    - FLUX.
    - euler normal la cap.
    - teorema celor 6/5 culori.
    - dist levenstein (usor).
*******************/
int main()
{
    flux::run();
}