Pagini recente » Cod sursa (job #311470) | Cod sursa (job #421451) | Cod sursa (job #1773377) | Cod sursa (job #2083351) | Cod sursa (job #123472)
Cod sursa(job #123472)
#include <cstdio>
#include <algorithm>
#define pow(x) (1u<<(x))
#define sqr(x) ((x)*(x))
#define inf (1<<30)
using namespace std;
int x[20], y[20], px[20], py[20], plus[20], n, nrpos;
int sol[17], dist[17][32], ord[17][32], ok[32], ind;
int comp(int a, int b){
return dist[ind][a] < dist[ind][b];
}
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 j=0; j<n; j++){
int i = ord[now][j];
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++){
for (int j=0; j<n; j++)
ord[i][j] = j;
ind = i;
sort(ord[i], ord[i]+n, comp);
}
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;
}
}
}