Pagini recente » Cod sursa (job #760491) | Cod sursa (job #676482) | Cod sursa (job #476269) | Cod sursa (job #2636647) | Cod sursa (job #2940823)
#include <fstream>
using namespace std;
ifstream cin("bibel.in");
ofstream cout("bibel.out");
const int NMAX = 17;
struct punct{
long long x, y;
};
punct v[NMAX + 1];
long long dp[(1 << (NMAX + 1)) + 1][NMAX + 1], n;
long long dist(punct a, punct b){
return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
void solve(){
/*
dp[mask][j] = distanta minima pt a lua bilele din masca, ultima bila luata fiind cea cu nr j
*/
long long ans = 2e18;
dp[1][1] = dist(v[0], v[1]);
dp[2][2] = dist(v[0], v[2]);
for(int i = 3; i < (1 << n); i++){
for(int j = 1; j <= n; j++){
if((i & (1 << (j - 1))) == 0)
continue;
long long cmin = 2e18;
for(int p = 1; p <= n; p++){
if((i & (1 << (p - 1))) == 0 || p == j)
continue;
int new_mask = (i ^ (1 << (j - 1)));
cmin = min(cmin, dp[new_mask][p] + dist(v[p], v[j]));
}
dp[i][j] = cmin;
}
}
for(int i = 1; i <= n; i++)
ans = min(ans, dp[(1 << n) - 1][i]);
cout << ans << "\n";
}
int main(){
ios :: sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int idk = 0, x = 0, y = 0;
while(cin >> idk && idk != 2){
if(idk == 1){
solve();
continue;
}
cin >> x >> y;
n++;
v[n].x = x;
v[n].y = y;
}
return 0;
}