Pagini recente » Cod sursa (job #2493774) | Cod sursa (job #1240867) | Cod sursa (job #732661) | Cod sursa (job #1644470) | Cod sursa (job #123470)
Cod sursa(job #123470)
#include <cstdio>
#include <string.h>
#define pow(x) (1u<<(x))
#define sqr(x) ((x)*(x))
#define inf (1<<30)
int x[20], y[20], px[20], py[20], plus[20], n, nrpos;
int sol[17], dist[17][32], ok[32];
int bound(int now){
int rez = 0;
ok[now] = 0;
for (int i=0; i<n; i++)
if (!ok[i] && i!=now){
int plus = inf;
for (int j=0; j<n; j++)
if (!ok[j] && j!=i && dist[i][j]<plus) plus = dist[i][j];
rez += plus;
}
ok[now] = 1;
return rez;
}
void back(int now, int mai, int cost){
if (!mai){
if (cost < sol[now]) sol[now] = cost;
return;
}
ok[now] = 1;
int opt = bound(now), bun=0;
for (int i=0; i<n; i++)
if (!ok[i] && cost+opt<sol[i]) bun=1;
if (bun)
for (int i=0; i<n; i++)
if (!ok[i]) back(i, mai-1, cost+dist[now][i]);
ok[now] = 0;
}
void solve(){
for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
dist[i][j] = (sqr(x[i]-x[j]) + sqr(y[i]-y[j]));
for (int i=0; i<n; i++)
sol[i] = inf;
for (int i=0; i<n; i++){
int best = inf;
for (int j=0; j<nrpos; j++)
if (sqr(px[j]-x[i]) + sqr(py[j]-y[i]) + plus[j]<best)
best = sqr(px[j]-x[i]) + sqr(py[j]-y[i]) + plus[j];
back(i, n-1, best);
}
nrpos = n;
int min=sol[0];
for (int i=0; i<n; i++){
if (sol[i] < min) min = sol[i];
px[i]=x[i], py[i]=y[i], plus[i] = sol[i];
}
printf("%d\n", min);
n=0;
}
int main()
{
freopen("bibel.in", "r", stdin);
freopen("bibel.out", "w", stdout);
nrpos=1;
while (1){
int op;
scanf("%d", &op);
if (op==0) scanf("%d %d", x+n, y+n), n++; else
if (op==1) solve(); else return 0;
}
}