Pagini recente » Cod sursa (job #2468694) | Cod sursa (job #2557942)
#include <bits/stdc++.h>
using namespace std;
const int N = 17;
int x[N + 1], y[N + 1];
long long dp[(1<<N) + 7][N + 1];
int xl[N + 1], yl[N + 1], drum[N + 1];
int main()
{
ifstream fin("bibel.in");
ofstream fout("bibel.out");
ios_base::sync_with_stdio(NULL);
fin.tie(0);
fout.tie(0);
int val, nlast(1);
xl[0] = yl[0] = 0;
drum[0] = 0;
while (1) {
int ind(0);
while (fin >> val) {
if (val == 2)
return 0;
if (val == 1)
break;
fin >> x[ind] >> y[ind];
ind++;
}
for (int msk = 0; msk < (1<<ind); ++msk)
for (int bit = 0; bit < ind; ++bit)
dp[msk][bit] = 1e13 + 7;
for (int bit = 0; bit < ind; ++bit) {
for (int last = 0; last < nlast; ++last)
dp[1<<bit][bit] = drum[last] + (x[bit] - xl[last]) * (x[bit] - xl[last]) + (y[bit] - yl[last]) * (y[bit] - yl[last]);
}
for (int msk = 1; msk < (1<<ind) - 1; ++msk) {
for (int bit = 0; bit < ind; ++bit) {
if (!((1<<bit)&msk))
continue;
for (int nou = 0; nou < ind; ++nou) {
if ((1<<nou)&msk)
continue;
dp[msk ^ (1<<nou)][nou] = min(dp[msk ^ (1<<nou)][nou], dp[msk][bit] + (x[bit] - x[nou]) * (x[bit] - x[nou]) + (y[bit] - y[nou]) * (y[bit] - y[nou]));
}
}
}
long long ans(1e11 + 7);
for (int i = 0; i < ind; ++i)
ans = min(ans, dp[(1<<ind) - 1][i]);
nlast = ind;
for (int i = 0; i < nlast; ++i)
drum[i] = dp[(1<<ind) - 1][i], xl[i] = x[i], yl[i] = y[i];
fout << ans << '\n';
}
return 0;
}