Cod sursa(job #2562191)

Utilizator stefan_creastaStefan Creasta stefan_creasta Data 29 februarie 2020 12:42:19
Problema Bibel Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.82 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int BMAX = 19;
const int INF = 0x7fffffff;
int dp[1 << BMAX][BMAX];
int antdp[BMAX];
struct Punct {
    int x, y;
};
Punct ant[BMAX];
Punct v[BMAX];
int dist(Punct a, Punct b) {
    int x = (a.x - b.x) * (a.x - b.x);
    int y = (a.y - b.y) * (a.y - b.y);
    return x + y;
}

int main() {
    int op;
    freopen("bibel.in", "r", stdin);
    freopen("bibel.out", "w", stdout);
    scanf("%d", &op);
    int m = 1;
    ant[1] = {0, 0};
    while(op != 2) {
        int n = 0;
        while(op != 1) {
            scanf("%d%d", &v[n].x, &v[n].y);
            n++;
            scanf("%d", &op);
        }
        for(int i = 0; i < (1 << n); i++) {
            for(int j = 0; j < n; j++) {
                dp[i][j] = INF;
            }
        }
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < m; j++) {
                dp[1 << i][i] = min(dp[1 << i][i], antdp[j] + ((v[i].x - ant[j].x) * (v[i].x - ant[j].x)) + ((v[i].y - ant[j].y) * (v[i].y - ant[j].y)));
            }
        }
        for(int i = 0; i < (1 << n); i++) {
            for(int j1 = 0; j1 < n; j1++) {
                if((i & (1 << j1)) != 0) {
                    for(int j2 = 0; j2 < n; j2++) {
                        if((i & (1 << j2)) == 0) {
                            dp[(i | (1 << j2))][j2] = min(dp[i][j2], dp[i][j1] + dist(v[j1], v[j2]));
                        }
                    }
                }
            }
        }
        int sol = INF;
        m = n;
        for(int i = 0; i < n; i++) {
            sol = min(sol, dp[(1 << n) - 1][i]);
            antdp[i] = dp[(1 << n) - 1][i];
            ant[i] = v[i];
        }
        printf("%d\n", sol);
        scanf("%d", &op);
    }
    return 0;
}