State compression DP Optimal pairing problem

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.