状态压缩dp 最优配对问题

在空间中的n(n为偶数)个点,配成n对,然后使得每一个点在一个点对中。所有的点对的距离之和最小

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#define INF 100000000

using 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;

		//依次枚举不同的集合
		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++){ //枚举其中一个点
				if(s & (1 << i)) break;
 			}
 			for(j = i+1;j < n;j++){ //再找另外一个点
 				if(s & (1 << j)){
 					//比较去掉这两个点的集合最小值的方法
 					d[s] = min(d[s],dist(i,j)+d[s^(1<<i)^(1<<j)]);
				 }
			 }
		}
		printf("%d\n",d[(1 << n)-1]);
	}
	return 0;
}

觉得的状态压缩dp适合于n较小但是状态异常复杂的dp环境

时间: 2024-08-04 03:49:04

状态压缩dp 最优配对问题的相关文章

状态压缩dp poj 3254 hdu5045

近来感觉状态压缩dp的强大性(灵活利用了二进制运算很关键)...于是做了俩提来看看..毕竟队友是专业的dp,我只是管中窥豹下而已..日后有机会再与之玩耍玩耍...ps:如果上天再给我一次机会,当年我愿意选择状态dp而不是网络流(只针对目前比赛出题潮流) 经典问题,不相邻/禁点方案数问题.poj3254 #include<iostream> #include<cstdio> using namespace std; int n,m; int dp[5000][15]; int yu[

hdu4753 状态压缩dp博弈(记忆化搜索写法)

http://acm.hdu.edu.cn/showproblem.php?pid=4753 Problem Description There is a 3 by 3 grid and each vertex is assigned a number. It looks like JiuGongGe, but they are different, for we are not going to fill the cell but the edge. For instance, adding

2010辽宁省赛E(Bellman_Ford最短路,状态压缩DP【三进制】)

#include<bits/stdc++.h>using namespace std;const int inf=0x3f3f3f3f;struct node{    int v,z,d,next;//存可以连接的点,用next存邻接表}a[10010];struct road{    int u,cnt,dis;//dis储存当前需要的钱数,即最短路算法里的权,u储存顶点,cnt储存组合数即状态压缩dp    road(int uu,int cntt,int diss)    {      

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

HDU1565(状态压缩dp)

方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8170    Accepted Submission(s): 3095 Problem Description 给你一个n*n的格子的棋盘,每个格子里面有一个非负数.从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出的数

HDU 3001【状态压缩DP】

题意: 给n个点m条无向边. 要求每个点最多走两次,要访问所有的点给出要求路线中边的权值总和最小. 思路: 三进制状态压缩DP,0代表走了0次,1,2类推. 第一次弄三进制状态压缩DP,感觉重点是对数据的预处理,利用数组分解各个位数,从而达到类似二进制的目的. 然后就是状态的表示,dp[s][i]表示状态s时到达i的最优值. 状态转移也一目了然,不废话. #include<stdio.h> #include<string.h> #include<algorithm> u

Victor and World(spfa+状态压缩dp)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5418 Victor and World Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 958    Accepted Submission(s): 431 Problem Description After trying hard fo

poj 3311 Hie with the Pie(状态压缩dp)

Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be

HDU--1074(状态压缩DP)

典型的状态压缩DP,给出了每件作业的截止时间和花费,求让老师扣分最少的写作业方式.把完成n种作业用状态2^n-1表示,dp[s]表示 完成状态s时,最小扣分.比如“111”,那么可以由“011”,“110”,“101”转移过来,分别表示选了0,1号作业,1,2号作业,0,2号作业. t[s]表示状态S记录的总时间.dp[s] = min{dp[j]+c[k] - d[k]},其中j = i^(1<<k),0<k<n;pre[s]表示状态s完成时,最末尾完成的作业, #include