Cod sursa(job #2940822)

Utilizator Theodor17Pirnog Theodor Ioan Theodor17 Data 16 noiembrie 2022 16:41:25
Problema Bibel Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <fstream>

using namespace std;

ifstream cin("bibel.in");
ofstream cout("bibel.out");

const int NMAX = 17;

struct punct{

    int x, y;

};

punct v[NMAX + 1];
long long dp[1 << (NMAX + 1) + 1][NMAX + 1], n;

long long dist(punct a, punct b){
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}

void solve(){

    /*

        dp[mask][j] = distanta minima pt a lua bilele din masca, ultima bila luata fiind cea cu nr j

    */

    long long ans = 2e18;

    dp[1][1] = dist(v[0], v[1]);
    dp[2][2] = dist(v[0], v[2]);
    for(int i = 3; i < (1 << n); i++){
        for(int j = 1; j <= n; j++){

            if((i & (1 << (j - 1))) == 0)
                continue;

            long long cmin = 2e18;

            for(int p = 1; p <= n; p++){

                if((i & (1 << (p - 1))) == 0 || p == j)
                    continue;

                int new_mask = (i ^ (1 << (j - 1)));
                cmin = min(cmin, dp[new_mask][p] + dist(v[p], v[j]));
            }

            dp[i][j] = cmin;
        }
    }

    for(int i = 1; i <= n; i++)
        ans = min(ans, dp[(1 << n) - 1][i]);

    cout << ans << "\n";
}

int main(){

    ios :: sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int idk = 0, x = 0, y = 0;
    while(cin >> idk && idk != 2){

        if(idk == 1){

            solve();
            continue;

        }


        cin >> x >> y;
        n++;

        v[n].x = x;
        v[n].y = y;

    }

    return 0;
}