刘汝佳训练指南P61
这里第二个for里的break保证了不出现重复组合
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include <string.h>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<time.h>
using namespace std;
#define MAX_N 110
#define inf 0x3f3f3f3f
#define LL long long
#define ull unsigned long long
const LL INF = 1e18;
const int mod = 1e8+7;
typedef pair<LL, LL>P;
double dp[1<<20];
struct node
{
double x, y, z;
};
double dis(node a, node b)
{
double dx = a.x - b.x;
double dy = a.y - b.y;
double dz = a.z - b.z;
return sqrt(dx*dx+dy*dy+dz*dz);
}
node p[25];
int main()
{
int n;
while(cin >> n && n) {
for(int i=0; i<n; i++)
cin >> p[i].x >> p[i].y >> p[i].z;
dp[0] = 0;
for(int i=1; i<(1<<n); i++) {
dp[i] = inf;
for(int j=0; j<n; j++) {
if(i & (1<<j)) {
for(int k=j+1; k<n; k++) {
if(i & (1<<k))
dp[i] = min(dp[i], dp[i^(1<<j)^(1<<k)]+dis(p[j], p[k]));
}
break;//注意此处
}
}
}
cout << dp[(1<<n)-1] << endl;
}
}