CodeForces - 8C Looking for Order【状态压缩dp】

23 篇文章 0 订阅

【题目描述】
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.

【输入】
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.

【输出】
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.

【样例输入】
0 0
2
1 1
-1 1

【样例输出】
8
0 1 2 0

【样例输入】
1 1
3
4 3
3 4
0 0

【样例输出】
32
0 1 2 0 3 0

题目链接:https://codeforces.com/contest/8/problem/C

代码如下:

#include <bits/stdc++.h>
using namespace std;
static const int INF=0x3f3f3f3f;
struct Node{
    int x,y;
}num[25];
int dis[25][25];
int dp[1<<24],pre[1<<24];
int calc(Node p,Node q)
{
    return (p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y);
}
int main()
{
    Node temp;
    cin>>temp.x>>temp.y;
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>num[i].x>>num[i].y;
    num[n]=temp;
    int len=(1<<n)-1;
    for(int i=0;i<=n;i++)
        for(int j=0;j<=n;j++)
            dis[i][j]=calc(num[i],num[j]);
    memset(dp,0x3f,sizeof(dp));
    dp[0]=0;
    for(int i=0;i<=len;i++)
        if(dp[i]!=INF)
            for(int j=0;j<n;j++)
                if(!(i&(1<<j)))
                {
                    if(dp[i|(1<<j)]>dp[i]+dis[j][n]+dis[n][j])
                    {
                        dp[i|(1<<j)]=dp[i]+dis[j][n]+dis[n][j];
                        pre[i|(1<<j)]=i;
                    }
                    for(int k=j+1;k<n;k++)
                        if(!((i|(1<<j))&(1<<k)))
                            if(dp[(i|(1<<j))|(1<<k)]>dp[i]+dis[j][k]+dis[k][n]+dis[n][j])
                            {
                                dp[(i|(1<<j))|(1<<k)]=dp[i]+dis[j][k]+dis[k][n]+dis[n][j];
                                pre[(i|(1<<j))|(1<<k)]=i;
                            }
                    break;
                }
    cout<<dp[len]<<endl;
    cout<<0;
    while(len)
    {
        for(int i=0;i<n;i++)
            if((len^pre[len])&(1<<i)) cout<<" "<<i+1;
        cout<<" "<<0;
        len=pre[len];
    }
    cout<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值