首页 > 代码库 > 点集配对问题

点集配对问题

点集配对问题  空间里n个点,使它们配成n/2对点,使得每个点恰好在一个点对中。

要求所有点队中,两点距离之和尽量下  n <= 20


d(s) = min(d{S - {i} - {j}+ |Pi Pj|  | j属于S, j > i, i = min{S}}


//#pragma comment(linker, "/STACK:102400000,102400000")
//HEAD
#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm>

#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <cstdlib>

using namespace std;
//LOOP
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
//STL
#define PB push_back
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RS(s) scanf("%s", s)
typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 1010;


#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FD(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define CPY(a, b) memcpy(a, b, sizeof(a))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define EQ(a, b) (fabs((a) - (b)) <= 1e-10)
#define ALL(c) (c).begin(), (c).end()
#define SZ(V) (int)V.size()
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define WI(n) printf("%d\n", n)
#define WS(s) printf("%s\n", s)
#define sqr(x) (x) * (x)
typedef vector <int> VI;
typedef unsigned long long ULL;
const double eps = 1e-10;
const LL MOD = 1e9 + 7;

int inx[25], iny[25], inz[25];
double dp[1 << 20];
int n;

double dis(int x, int y)
{
    return sqrt(sqr(inx[x] - inx[y]) + sqr(iny[x] - iny[y]) + sqr(inz[x] - inz[y]));
}

double dfs(int x)
{
    if (dp[x] < INF)    return dp[x];
    int i, u = -1, v = -1;
    for (i = 0; i < n; i++)
        if ((x & (1 << i)) == 0)
            { u = i;   break;}
    for (int j = i + 1; j < n; j++)
    {
        if ((x & (1 << j)) == 0)
            dp[x] = min(dfs(x | (1 << j) | (1 << u)) + dis(u, j), dp[x]);
    }
    return dp[x];
}

int main()
{
    while (~RI(n))
    {
        int x, y, z;
        REP(i, n)
            RIII(inx[i], iny[i], inz[i]);
        REP(i, 1 << n) dp[i] = INF;
        dp[(1 << n) - 1] = 0;
        dfs(0);
        printf("%.10lf\n", dp[0]);
    }
    return 0;
}