Looking for Order (状压DP)

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,jn 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,n1] [ 0 , n − 1 ] 一层循环枚举看哪个点 i i 没选,就选上,然后里面在一层循环枚举选取第二个没被选取的点j,注意当 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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的公寓报修管理系统,源码+数据库+毕业论文+视频演示 现代经济快节奏发展以及不断完善升级的信息化技术,让传统数据信息的管理升级为软件存储,归纳,集中处理数据信息的管理方式。本公寓报修管理系统就是在这样的大环境下诞生,其可以帮助管理者在短时间内处理完毕庞大的数据信息,使用这种软件工具可以帮助管理人员提高事务处理效率,达到事半功倍的效果。此公寓报修管理系统利用当下成熟完善的Spring Boot框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的MySQL数据库进行程序开发。公寓报修管理系统有管理员,住户,维修人员。管理员可以管理住户信息和维修人员信息,可以审核维修人员的请假信息,住户可以申请维修,可以对维修结果评价,维修人员负责住户提交的维修信息,也可以请假。公寓报修管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 关键词:公寓报修管理系统;Spring Boot框架;MySQL;自动化;VUE
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值