Looking for Order
Girl Lena likes it when everything is in order, and looks for order everywhere. Once she was getting ready for the University and noticed that the room was in a mess — all the objects from her handbag were thrown about the room. Of course, she wanted to put them back into her handbag. The problem is that the girl cannot carry more than two objects at a time, and cannot move the handbag. Also, if he has taken an object, she cannot put it anywhere except her handbag — her inherent sense of order does not let her do so.
You are given the coordinates of the handbag and the coordinates of the objects in some Сartesian coordinate system. It is known that the girl covers the distance between any two objects in the time equal to the squared length of the segment between the points of the objects. It is also known that initially the coordinates of the girl and the handbag are the same. You are asked to find such an order of actions, that the girl can put all the objects back into her handbag in a minimum time period.
Input
The first line of the input file contains the handbag's coordinates xs, ys. The second line contains number n (1 ≤ n ≤ 24) — the amount of objects the girl has. The following n lines contain the objects' coordinates. All the coordinates do not exceed 100 in absolute value. All the given positions are different. All the numbers are integer.
Output
In the first line output the only number — the minimum time the girl needs to put the objects into her handbag.
In the second line output the possible optimum way for Lena. Each object in the input is described by its index number (from 1 to n), the handbag's point is described by number 0. The path should start and end in the handbag's point. If there are several optimal paths, print any of them.
Examples
Input
0 0
2
1 1
-1 1
Output
8
0 1 2 0
Input
1 1
3
4 3
3 4
0 0
Output
32
0 1 2 0 3 0
题意:
给n个物品的坐标, 和一个包裹的位置, 包裹不能移动。 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程。
分析:
首先我们会发现,如果我们选两个点 i,j拿的话,起点为n i , j 拿 的 话 , 起 点 为 n 那么先拿i还是先拿j是没有影响的
即他们距离总是 dis[n][i]+dis[i][j]+dis[j][n] d i s [ n ] [ i ] + d i s [ i ] [ j ] + d i s [ j ] [ n ]
而且如果要选择这两个点的话,即每两个点若先事先分好哪两个点一起拿的话,那么点对的选取先后顺序同样不会影响结果
因此想到使用状压dp枚举状态,而这个状态就是只代表选与不选,与顺序无关
首先朴素的思想是枚举每种二进制状态
然后 [0,n−1] [ 0 , n − 1 ] 一层循环枚举看哪个点 i i 没选,就选上,然后里面在一层循环枚举选取第二个没被选取的点,注意当 i=j i = j 时,相当于选取的一个点,然后进行状态转移
当时刚才我们提到了选取点顺序无关,而我们如果像上面那样循环的话,必然会出现i,j和j,i这样就出现了重复,没有必要,因此枚举第二个点的循环从第一个点的位置往后再枚举就行了
但是这样选取点的复杂度仍然是 O(n2) O ( n 2 ) 再加上状态枚举总的复杂度是 O(2n×n2) O ( 2 n × n 2 )
实际上在枚举了一种状态下,我们只需要选取第一个没选的点进行状态转移了,仍然是我们上面分析的,如果已经分好了点对,即哪两个点一起选,那么点对的选取先后顺序也不会对结果产生影响,而且,枚举后面的状态的时候一定会选取到,也就是我们固定了点对的按第一个点编号从小到大选取顺序,这样只需要枚举到第一个没选取的点即可
复杂度降为 O(2n×n) O ( 2 n × n )
最后一定要注意按位运算符和==的优先级,==优先级大于&,所以一定注意加括号
code:
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int dp[1<<24],dis[25][25],pres[1<<24],path[550],cnt,n;
pair<int,int> a[25];
//dp[i]代表i状态下的最小值
//dis[i][j]表示i点到j点的距离
//pres[i]表示i状态的前一个状态是pres[i]状态
//path记录路径
int main(){
int x,y;
scanf("%d%d",&x,&y);
scanf("%d",&n);
a[n] = make_pair(x,y);
for(int i = 0; i < n; i++){
scanf("%d%d",&x,&y);
a[i] = make_pair(x,y);
}
//预处理出每个点间的距离
for(int i = 0; i <= n; i++){
for(int j = 0; j <= n; j++){
dis[i][j] = (a[i].first - a[j].first) * (a[i].first - a[j].first) + (a[i].second - a[j].second) * (a[i].second - a[j].second);
}
}
memset(dp,inf,sizeof(dp));
dp[0] = 0;
for(int i = 0; i < (1 << n); i++){
if(dp[i] == inf) continue;
for(int j = 0; j < n; j++){
if(((1 << j) & i) == 0){//如果j点没有拿,就拿
for(int k = j; k < n; k++){//从j开始往后再拿一个,避免了重复
if(((1 << k) & i) == 0){
int tmp = i | (1 << j) | (1 << k);
int d = dis[j][k] + dis[n][j] + dis[n][k];
//特殊情况:当k=j相当于选取了一个点,此时就增加了j这个点,新增加的距离就是n->j,j->n即2*dis[n][j]
//其他情况:当k!=j相当于选取了两个点,此时新增加的距离为三段dis[j][k] + dis[n][j] + dis[n][k]
if(dp[tmp] > dp[i] + d){
dp[tmp] = dp[i] + d;//有点像最短路松弛操作
pres[tmp] = i;//记录下新状态的前一个状态
}
}
}
break;//对于每个枚举的状态我们只需要进行一次转移降低复杂度
//即该状态下找到第一个没选取的点进行转移,后面点的选取就先不用枚举了
//因为顺序不会影响结果,所以在枚举后面的状态的时候肯定还会枚举到
}
}
}
printf("%d\n",dp[(1<<n)-1]);
for(int i = (1<<n)-1; i != 0; i = pres[i]){
int tmp = pres[i] ^ i;//得到当前状态比前一个状态多的点
path[cnt++] = 0;//每次先把起点放入
for(int j = 0; j < n; j++){//枚举看看是哪个点
if((1 << j) & tmp){
path[cnt++] = j + 1;
}
}
}
path[cnt++] = 0;
//因为是逆着存的所以逆着输出就是正向路径
for(int i = cnt-1; i >= 0; i--){
printf("%d ",path[i]);
}
return 0;
}