标签:
状压以后,直接暴力枚举,2^20约等于1e6,而且满足bitcount = m的状态很少,很稳
#include<bits/stdc++.h> using namespace std; const int maxn = 20+1; double x[maxn],y[maxn],z[maxn]; double d[maxn][maxn]; double squ(double x) { return x*x; } double dist(int i,int j) { return squ(x[i]-x[j])+squ(y[i]-y[j])+squ(z[i]-z[j]); } //#define LOCAL int main() { #ifdef LOCAL freopen("in.txt","r",stdin); #endif int n,m; scanf("%d%d",&n,&m); for(int i = 0; i < n; i++){ scanf("%lf%lf%lf",x+i,y+i,z+i); } for(int i = 0; i < n; i++){ for(int j = 0; j < i; j++){ d[i][j] = dist(i,j); } } double ans = 0; int vc[maxn]; for(int S = (1<<n)-1; S>=0; S--){ int c = 0; for(int i = 0; i < n; i++){ if(S>>i&1) vc[c++] = i; } if(c == m){ double sum = 0; for(int i = 0; i < c; i++){ int a = vc[i]; for(int j = 0; j < i; j++){ sum += d[a][vc[j]]; } } ans = max(ans,sum); } } printf("%lf\n",ans); return 0; }
标签:
原文地址:http://www.cnblogs.com/jerryRey/p/4851297.html