Cod sursa(job #2961845)

Utilizator Andrews69Harnagea Andrei-Alexandru Andrews69 Data 7 ianuarie 2023 03:20:24
Problema Taramul Nicaieri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <climits>
//O(N*M^2)
using namespace std;
ifstream f("harta.in");
ofstream g("harta.out");

vector<vector<int>> la;
int cap[201][201], s, t,n;
vector<int> tata;

bool bfs(){
        //BFS pentru a verifica fluxul maxim
    vector<bool> viz(t);
    queue<int> coada;
    coada.push(s);
    viz[s]=true;
    tata[s]=-1;
    while(!coada.empty())
    {
        int u=coada.front();
        coada.pop();
        for(auto v:la[u])
        {
            if(viz[v]==false && cap[u][v]!=0)
            {
                tata[v]=u;
                if(v==t)
                    return true;
                viz[v]=true;
                coada.push(v);
            }
        }
    }
    return false;
}

int EdomondsKarp(){
    long fluxmax = 0;
    while(bfs()==true){

        int u, v=t;
        int flux = INT_MAX;
        while(v!=s){
            u=tata[v];
            if(cap[u][v]<flux)
                flux=cap[u][v];

            v=tata[v];
        }
        v=t;
        while(v!=s){
            u=tata[v];
            cap[v][u]+=flux;
            cap[u][v]-=flux;
            v=tata[v];
        }

        fluxmax+=flux;
    }
    return fluxmax;

}

int main() {

    f>>n;
    s = 0;
    t = 2*n+1;
    la.resize(201);
    tata.resize(2*n+1);

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i!=j){
                la[i].push_back(j+n);
                la[j+n].push_back(i);
                cap[i][j+n]=1;
            }
        }
    }

    for(int i=1;i<=n;i++){
        int x, y;
        f>>x>>y;
        la[s].push_back(i);
        la[i].push_back(s);
        cap[s][i] = x;
        la[t].push_back(i+n);
        la[i+n].push_back(t);
        cap[i+n][t]=y;
    }
/*
    for(int i=1;i<=n+n;i++){
        for(int j=1;j<=n+n;j++){
            cout<<la[i][j]<<" ";
        }
        cout<<endl;
    }
*/

    g<<EdomondsKarp()<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(cap[j+n][i]==1){
                g<<i<<" "<<j<<endl;
            }
        }
    }

    return 0;
}