Cod sursa(job #2641524)

Utilizator Iulia14iulia slanina Iulia14 Data 11 august 2020 18:42:30
Problema Bibel Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <fstream>

using namespace std;
ifstream cin ("bibel.in");
ofstream cout ("bibel.out");
struct ura{
    int x, y;
};
ura v[20];
long long dp[1 << 17][20];
ura vv[20];
long long t[20];
long long dist(int x1, int x2, int y1, int y2)
{
    return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int main()
{
    int n = 0, nr, ok = 0, nn = 1;
    while (ok == 0)
    {
        cin >> nr;
        if (nr == 1)
        {
            int i, j, h;
            long long sol = 1e18;
            for (i = 0; i < nn; i++)
                t[i] = dp[(1 << nn) - 1][i];
            for (i = 0; i < (1 << n); i++)
                for (j = 0; j < n; j++)
                    dp[i][j] = 1e18;
            for (i = 0; i < nn; i++)
                for (j = 0; j < n; j++)
                    dp[1 << j][j] = min (dp[1 << j][j], t[i] + dist(vv[i].x, v[j].x, vv[i].y, v[j].y));
            for (i = 0; i < (1 << n); i++)
                for (j = 0; j < n; j++)
                    if (i & (1 << j))
                        for (h = 0; h < n; h++)
                            if (i & (1 << h) && j != h)
                                dp[i][j] =  min(dp[i][j], dp[i ^ (1 << j)][h] + dist(v[h].x, v[j].x, v[h].y, v[j].y));
            for (i = 1; i <= n; i++)
                sol = min(sol, dp[(1 << n) - 1][i - 1]);
            cout << sol << '\n';
            for (i = 1; i <= n; i++)
                vv[i] = v[i];
            nn = n;
            n = 0;
        }
        if (nr == 0)
        {
            cin >> v[n].x >> v[n].y;
            n++;
        }
        if (nr == 2)
            ok = 1;
    }
    return 0;
}