Cod sursa(job #2769447)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 15 august 2021 19:01:23
Problema Cc Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.81 kb
#include <bits/stdc++.h>
#define NMAX 205
#define INF 0x3f3f3f3f

using namespace std;

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

int n, m, t[NMAX], C[NMAX][NMAX], F[NMAX][NMAX], dist[NMAX][NMAX], d[NMAX];
bitset <NMAX> iq;
vector <int> edges[NMAX];
queue <int> q;

bool bf()
{
    bool ok = 0;
    memset(d, INF, sizeof(d));
    memset(t, 0, sizeof(t));
    iq.reset();

    d[0] = 0;
    iq[0] = 1;
    q.push(0);

    while(!q.empty())
    {
        int nod = q.front();
        q.pop();
        iq[nod] = 0;
        for(auto child : edges[nod])
            if(C[nod][child] > F[nod][child] && d[child] > d[nod] + dist[nod][child])
            {
                d[child] = d[nod] + dist[nod][child];
                t[child] = nod;
                if(!iq[child])
                {
                    iq[child] = 1;
                    q.push(child);
                }
            }
    }
    return (d[2 * n + 1] != INF);
}

int main()
{
    f >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
        {
            int cost;
            f >> cost;
            dist[i][j + n] = cost;
            dist[j + n][i] = -cost;
            C[i][j + n] = 1;
            edges[i].push_back(j + n);
            edges[j + n].push_back(i);
        }

    for(int i = 1; i <= n; i++)
    {
        C[0][i] = 1;
        edges[0].push_back(i);
        edges[i].push_back(0);

        C[i + n][2 * n + 1] = 1;
        edges[i + n].push_back(2 * n + 1);
        edges[2 * n + 1].push_back(i + n);
    }

    int cost = 0;
    while(bf())
    {
        int k = 2 * n + 1;
        while(k != 0)
        {
            F[t[k]][k] ++;
            F[k][t[k]] --;
            k = t[k];
        }
        cost += d[2 * n + 1];
    }

    g << cost;

    return 0;
}