Nu aveti permisiuni pentru a descarca fisierul grader_test8.in

Cod sursa(job #2562414)

Utilizator stefan_creastaStefan Creasta stefan_creasta Data 29 februarie 2020 14:11:53
Problema Bibel Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2 kb
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int BMAX = 19;
const int INF = 1 << 28;
int dp[1 << BMAX][BMAX + 2];
struct Punct {
    int x, y;
};
struct Ant {
    Punct p;
    int cost;
};
int dist(Punct a, Punct b) {
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
Punct v[BMAX];
vector <Ant> a;

int main() {
    freopen("bibel.in", "r", stdin);
    freopen("bibel.out", "w", stdout);
    int op;
    scanf("%d", &op);
    int n = 0;
    long long sol = 0;
    a.push_back({{0, 0}, 0});
    while(1) {
        if(op == 2) {
            return 0;
        }
        if(op == 1) {
            for(int i = 1; i < (1 << n); i++) {
                for(int j = 0; j < n; j++) {
                    dp[i][j] = INF;
                }
            }
            for(int j = a.size() - 1; j >= 0; j--) {
                for(int i = 0; i < n; i++) {
                    dp[1 << i][i] = min(dp[1 << i][i], a[j].cost + dist(a[j].p, v[i]));
                }
                a.pop_back();
            }
            for(int i = 1; i < (1 << n); i++) {
                for(int j = 0; j < n; j++) {
                    if(i & (1 << j)) {
                        for(int p = 0; p < n; p++) {
                            if(p != j && (i & (1 << p))) {
                                dp[i][p] = min(dp[i][p], dp[i ^ (1 << p)][j] + dist(v[j], v[p]));
                            }
                        }
                    }
                }
            }
            int nrc = INF;
            for(int i = 0; i < n; i++) {
                nrc = min(nrc, dp[(1 << n) - 1][i]);
            }
            sol += nrc;
            for(int i = 0; i < n; i++) {
                a.push_back({v[i], dp[(1 << n) - 1][i] - nrc});
            }
            printf("%lld\n", sol);
            n = 0;
        }
        else {
            scanf("%d%d", &v[n].x, &v[n].y);
            n++;
        }
        scanf("%d", &op);
    }
    return 0;
}