codeforces 8C(非原创)

C. Looking for Order
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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个物品,一个人没次最多带两个物品,问这个人从起点出发,把所有物品拿到起点最少走过平方距离。

 

解题思路:就是把所有情况用二进制表示,在此基础上进行dp。这题的dp部分很简单,就是在上个状态的基础上更新拿物品i的总值。

参考博客:http://blog.csdn.net/mrsiz/article/details/48174943

附ac代码(有详细注释):

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <string>
 4 #include <string.h>
 5 #include <cmath>
 6 #include <iostream>
 7 #include <algorithm>
 8 #include <queue>
 9 #include <stack>
10 #include <vector>
11 typedef long long ll;
12 using namespace std;
13 const int maxn = 111;
14 const int inf = 0x3f3f3f3f;
15 struct nod{
16     int x;
17     int y;
18 }nu[maxn],bg;
19 int getd(nod a,nod b)
20 {
21     return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
22 }
23 int dp[1<<24];
24 int dis[maxn][maxn];
25 int pre[1<<24];
26 int main() {
27     ios::sync_with_stdio(false);
28     int n;
29     cin>>bg.x>>bg.y;
30     cin>>n;
31     int len=(1<<n)-1;
32     for(int i=0;i<n;++i)
33     {
34         cin>>nu[i].x>>nu[i].y;
35     }
36     nu[n].x=bg.x;
37     nu[n].y=bg.y;
38     for(int i=0;i<=n;++i)
39     {
40         for(int j=0;j<=n;++j)
41         {
42             dis[i][j]=getd(nu[i],nu[j]);
43         }
44     }
45     
46     int maxx=1<<24;
47     for(int i=1;i<maxx;++i) dp[i]=inf;
48     for(int i=0;i<=len;++i)   //从一个都没拿的状态开始向后遍历
49     {
50         if(dp[i]!=inf)
51         {
52             for(int j=0;j<n;++j)
53             {
54                 if(!(i&(1<<j)))
55                 {
56                     int t=i|(1<<j);//i的基础上拿了j物品的状态
57                     int l=dp[i]+dis[n][j]+dis[j][n];
58                     if(dp[t]>l)
59                     {
60                         dp[t]=l;//更新
61                         pre[t]=i;//记录路径
62                     }
63                     for(int k=j+1;k<n;++k)//j的基础上拿了k物品的状态
64                     {
65                         if(!(t&(1<<k)))
66                         {
67                             int p=t|(1<<k);
68                             int l=dp[i]+dis[n][j]+dis[j][k]+dis[k][n];
69                             if(dp[p]>l)
70                             {
71                                 dp[p]=l;
72                                 pre[p]=i;
73                             }
74                         }
75                     }
76                     break;
77                 }
78             }
79         }
80         
81     }
82     
83     cout<<dp[len]<<endl;
84     cout<<0;
85     while(len>0)
86     {
87         for(int i=0;i<n;++i)    if((len^pre[len])&(1<<i))  cout<<" "<<i+1;
88         cout<<" "<<0;
89         len=pre[len];
90     }
91     
92     return 0;
93 }
View Code

 

转载于:https://www.cnblogs.com/zmin/p/8386014.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值