codeforces 8C(非原创)
4 seconds
512 megabytes
standard input
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.
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
题意:有平面上有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 }
codeforces 8C(非原创)的更多相关文章
- codeforces 6E (非原创)
E. Exposition time limit per test 1.5 seconds memory limit per test 64 megabytes input standard inpu ...
- Linux下high CPU分析心得【非原创】
非原创,搬运至此以作笔记, 原地址:http://www.cnitblog.com/houcy/archive/2012/11/28/86801.html 1.用top命令查看哪个进程占用CPU高ga ...
- CSS样式命名整理(非原创)
非原创,具体出自哪里忘了,如果侵害您的利益,请联系我. CSS样式命名整理 页面结构 容器: container/wrap 整体宽度:wrapper 页头:header 内容:content 页面主体 ...
- 非原创。使用ajax加载控件
非原创.来自博客园老赵. public class ViewManager<T> where T : System.Web.UI.UserControl { private System. ...
- Java 表达式解析(非原创)
因项目需要,在网上找来一套表达式解析方法,由于原来的方法太过于零散,不利于移植,现在整理在同一文件内: 文件中包含5个内部类,源码如下: import java.util.ArrayList; imp ...
- Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创)
Java Interface 是常量存放的最佳地点吗?(转帖学习,非原创) 由于java interface中声明的字段在编译时会自动加上static final的修饰符,即声明为常量.因而inter ...
- 用RD,GR,BL三个方法内代码生成一张图片(非原创,我只是完整了代码)
我公开以下图片的源代码,,是ppm格式的,,自己找到能打开的工具.. (非原创,我加工的代码,可直接执行运行输出,缩略图能看到效果) 这是原博客 http://news.cnblogs.com/n/ ...
- tp5.1 phpspreadsheet- 工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西,)
phpspreadsheet-工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西)1. composer require phpoffice/phpspreadsheet2. 看最下面的两 ...
- Vue 仿QQ左滑删除功能(非原创)
非原创,摘选来源:http://www.jb51.net/article/136221.htm. 废话不多说,相当实用,先记录. Html代码: <div class="contain ...
- 老男孩Django笔记(非原创)
.WEB框架 MVC Model View Controller 数据库 模板文件 业务处理 MTV Model Template View 数据库 模板文件 业务处理 ############## ...
随机推荐
- 翻译 - ASP.NET Core 基本知识 - 中间件(Middleware)
翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0 中间件是集成 ...
- 创建并使用https证书
目录 前言 产生证书 测试https服务器 用tls加密tcp连接 总结 前言 https要比http更安全些,因此可以配置Nginx服务器使用证书,客户端就会去第三方平台校验证书. 但是我们自己的服 ...
- (06)-Python3之--判断、循环
1.判断(if) 语法: if 条件(True/False): 条件为真时,执行的代码(要干的事情)[elif 条件: 条件为真时,执行的代码(要干的事情)elif 条件: 条件为真时,执行的代码(要 ...
- 记一次Nginx反向代理500的排查记录
今天公司项目遇到一个奇怪的问题,记录一下. 注: 数据已经过脱敏处理,未暴露公司具体的IP等数据. TLDR; 项目简单介绍 用 Vue + ElementUI 实现的后台项目(以下简称:a-proj ...
- 阿里巴巴微服务与配置中心技术实践之道 配置推送 ConfigurationManagement ConfigDrivenAnyting
阿里巴巴微服务与配置中心技术实践之道 原创: 坤宇 InfoQ 2018-02-08 在面向分布式的微服务系统中,如何通过更高效的配置管理方式,帮助微服务系统架构持续"无痛"的演进 ...
- hadoop 集群搭建 配置 spark yarn 对效率的提升永无止境 Hadoop Volume 配置
[手动验证:任意2个节点间是否实现 双向 ssh免密登录] 弄懂通信原理和集群的容错性 任意2个节点间实现双向 ssh免密登录,默认在~目录下 [实现上步后,在其中任一节点安装\配置hadoop后,可 ...
- CODEVS 2542单词__fail树
2542 单词 2013年省队选拔赛天津市队选拔赛 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 小张最近在忙毕 ...
- SpringMVC听课笔记(十:处理JSON: 使用HttpMessageConverter)
1. 处理JSON 2. 原理 流程图 3. 看个应用吧 -- 上传 ①jsp ②handler -- 下载 ① jsp ② handler
- Android webview 问题记录
1. Android 7.1真机安装调试apk时报错,解析安装包失败 原因:sdk版本不支持7.1版本,需要调整minSdkVersion等参数,支持低版本 解决方法: android { compi ...
- 关闭Linux - centos7的防火墙
关闭Centos7的防火墙 在每台虚拟机上分别执行以下指令: systemctl stop firewalld.service #停止firewall systemctl disable firewa ...