Pagini recente » Cod sursa (job #371866) | Cod sursa (job #49144) | Cod sursa (job #726362) | Cod sursa (job #63995) | Cod sursa (job #2945830)
#include <bits/stdc++.h>
#define X first
#define Y second
#define INF 1e18
#define int long long
using namespace std;
ifstream fin("bibel.in");
ofstream fout("bibel.out");
pair<int, int> a[205], last[205];
int n, dp[17][1 << 17], N, sol, b[205];
int Dist(pair <int, int> o, pair <int, int> p)
{
return (o.X - p.X) * (o.X - p.X) + (o.Y - p.Y) * (o.Y - p.Y);
}
int32_t main()
{
int op, x, ok = 1, i, j, masca, lastn = 1;
while (ok == 1)
{
fin >> op;
if (op == 2)
ok = 0;
else if (op == 0)
{
fin >> a[n].X >> a[n].Y;
n++;
}
else
{
for (i = 0; i < lastn; i++)
b[i] = dp[i][(1 << lastn) - 1];
N = (1 << n);
for (i = 0; i < n; i++)
for (j = 0; j < N; j++)
dp[i][j] = INF;
for (i = 0; i < n; i++)
for (j = 0; j < lastn; j++)
dp[i][1 << i] = min(dp[i][1 << i], b[j] + Dist(a[i], last[j]));
for (masca = 0; masca < N; masca++)
for (i = 0; i < n; i++)
if (masca & (1 << i))
{
x = masca - (1 << i);
for (j = 0; j < n; j++)
if (x & (1 << j))
dp[i][masca] = min(dp[i][masca], dp[j][x] + Dist(a[i], a[j]));
}
for (i = 1; i <= n; i++)
last[i] = a[i];
sol = INF;
for (i = 0; i < n; i++)
sol = min(sol, dp[i][N - 1]);
fout << sol << "\n";
lastn = n;
n = 0;
}
}
return 0;
}