N (n is even) points in space, paired with n pairs, then makes each point in a point pair. The minimum distance of all point pairs
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack > #include <climits> #include <cstring> #include <cmath> #include <map> #include <set># Define INF 100000000using namespace Std;int n;int x[1000];int y[1000];int d[1<< 21];int dist (int a,int b) {return (x[ A]-x[b]) * (X[a]-x[b]) + (Y[a]-y[b]) * (Y[a]-y[b]);} int main () {while (CIN >> N) {for (int i = 0;i < n;i++) {cin >> x[i] >> y[i];} Memset (d,0,sizeof (d)); for (int i = 0;i < (1<<n); i++) {d[i] = INF;} D[0] = 0;//Enumerate the different sets in turn for (int s = 0;s < (1<<n); s++) {int i,j;//d[s] = min (| pipj| + d[s-{i}-{j}]); for (i = 0;i < n;i++) {//Enumerate one of the points if (S & (1 << i)) Break,} for (j = I+1;j < n;j++) {//To find another point if (S & (1 << j)) {//Compare the method of removing the minimum set of these two points d[s] = min (d[s],dist (i,j) +d[s^ (1<<i) ^ (1<<j)]); }}}printf ("%d\n", d[(1 << N)-1]);} return 0;}
Feel the state of compression DP suitable for n small but abnormally complex DP environment
State compression DP Optimal pairing problem