Cod sursa(job #910832)

Utilizator dicu_dariaDaria Dicu dicu_daria Data 11 martie 2013 09:00:11
Problema Bibel Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.71 kb
#include <fstream>
#include <cstring>

using namespace std;

ifstream fi("bibel.in");
ofstream fo("bibel.out");
int pn, tip, n, i, j;
long long conf, sol, Pc[270000][20], Px[20], Py[20], X[20], Y[20], C[270000][20], Cost[20][20];


long long get_dist(int x1, int y1, int x2, int y2)
{
    return 1LL*(x1-x2)*(x1-x2) + 1LL*(y1-y2)*(y1-y2);
}

void solve()
{
    //stiu ca trebuie sa termin intai nivelul precedent
    n = 0;
    while(fi >> tip)
    {
        if(tip == 1) break;
        if(tip == 2) return;
        ++n;
        fi >> X[n] >> Y[n];
    }
    for(i = 1; i <= n; i++)
        for(j = i+1; j <= n; j++)
            Cost[i][j] = Cost[j][i] = get_dist(X[i], Y[i], X[j], Y[j]);
    for(i = 0; i <= (1<<n); i++)
        for(j = 0; j <= n; j++)
            C[i][j] = (1LL<<50);
    for(i = 0; i < n; i++)
        for(j = 0; j < pn; j++)
        {
            if(j == 0) C[1<<i][i] = (1LL<<50);
            C[1<<i][i] = min(C[1<<i][i], Pc[conf][j] + get_dist(X[i+1], Y[i+1], Px[j+1], Py[j+1]));
        }
    for(conf = 1; conf < (1 << n); conf++)
        for(i = 0; i < n; i++)
        if((1<<i) & conf) //daca exista i in configuratie
        {
            for(j = 0; j < n; j++)
            if(((1<<j)&conf) and i != j) //daca exista j in configuratie
            C[conf][i] = min(C[conf][i], Cost[i+1][j+1] + C[conf^(1<<i)][j]);
        }
    for(sol = (1LL<<50) , i = 0, conf = (1<<n) - 1; i < n; i++)
        sol = min(sol, C[conf][i]);
    fo << sol << "\n";
    memcpy(Px, X, sizeof(X));
    memcpy(Py, Y, sizeof(Y));
    memcpy(Pc, C, sizeof(C));
    pn = n;
    solve();
}



int main()
{
    pn = 1;
    Pc[1][0] = 1;
    Px[1] = Py[1] = 0;
    solve();
    return 0;
}