题目链接:

http://codeforces.com/problemset/problem/8/C

C. Looking for Order

time limit per test:4 seconds
memory limit per test:512 megabytes
#### 问题描述
> 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.

样例输入

1 1

3

4 3

3 4

0 0

样例输出

32

0 1 2 0 3 0

题意

给你垃圾桶的位置和垃圾的位置,你每次能从垃圾桶出发捡一到两个垃圾然后回到垃圾桶,问如何规划使得捡垃圾所花时间最短(时间是以两点距离平方为基准)

题解

状压dp,有点像最优顶点配对问题的算法来处理,时间复杂度为n*2^n。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=24; int dp[1<<maxn],n;
int pre[1<<maxn];
struct Node{
int a,b;
bool type;
}nds[1<<maxn]; PII pt[maxn]; int dis(int i,int j){
int a=pt[i].X-pt[j].X;
int b=pt[i].Y-pt[j].Y;
return a*a+b*b;
} int main() {
scf("%d%d",&pt[0].X,&pt[0].Y);
scf("%d",&n);
for(int i=1;i<=n;i++){
scf("%d%d",&pt[i].X,&pt[i].Y);
} clr(dp,0x7f);
dp[0]=0;
clr(pre,-1);
for(int stat=1;stat<(1<<n);stat++){
for(int i=0;i<n;i++){
if(stat&(1<<i)){
///第一个单独不匹配
if(dp[stat]>dp[stat^(1<<i)]+2*dis(0,i+1)){
dp[stat]=dp[stat^(1<<i)]+2*dis(0,i+1);
pre[stat]=stat^(1<<i);
nds[stat].a=i+1;
nds[stat].type=0;
}
dp[stat]=min(dp[stat],dp[stat^(1<<i)]+2*dis(0,i+1)); ///第一个和后面的某一个匹配
for(int j=0;j<n;j++){
if(j!=i&&stat&(1<<j)){
int tmp=dp[stat^(1<<i)^(1<<j)]+dis(0,i+1)+dis(i+1,j+1)+dis(0,j+1);
if(dp[stat]>tmp){
dp[stat]=tmp;
pre[stat]=stat^(1<<i)^(1<<j);
nds[stat].a=i+1; nds[stat].b=j+1;
nds[stat].type=1;
}
}
}
//这个减枝非常关键!和最优顶点配对的做法一样
break;
}
}
} VI ans;
ans.pb(0);
int p=(1<<n)-1;
while(p!=0){
if(nds[p].type==0){
ans.pb(nds[p].a);
}else{
ans.pb(nds[p].a);
ans.pb(nds[p].b);
}
ans.pb(0);
p=pre[p];
}
reverse(all(ans));
prf("%d\n",dp[(1<<n)-1]); rep(i,0,ans.sz()-1) prf("%d ",ans[i]);
prf("%d\n",ans[ans.sz()-1]); return 0;
} //end-----------------------------------------------------------------------

Codeforces Beta Round #8 C. Looking for Order 状压dp的更多相关文章

  1. Codeforces Beta Round #8 C. Looking for Order 状压

    C. Looking for Order 题目连接: http://www.codeforces.com/contest/8/problem/C Description Girl Lena likes ...

  2. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  3. Codeforces 453B Little Pony and Harmony Chest:状压dp【记录转移路径】

    题目链接:http://codeforces.com/problemset/problem/453/B 题意: 给你一个长度为n的数列a,让你构造一个长度为n的数列b. 在保证b中任意两数gcd都为1 ...

  4. [多校联考2019(Round 5 T1)] [ATCoder3912]Xor Tree(状压dp)

    [多校联考2019(Round 5)] [ATCoder3912]Xor Tree(状压dp) 题面 给出一棵n个点的树,每条边有边权v,每次操作选中两个点,将这两个点之间的路径上的边权全部异或某个值 ...

  5. CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP

    题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...

  6. Codeforces 1383C - String Transformation 2(找性质+状压 dp)

    Codeforces 题面传送门 & 洛谷题面传送门 神奇的强迫症效应,一场只要 AC 了 A.B.D.E.F,就一定会把 C 补掉( 感觉这个 C 难度比 D 难度高啊-- 首先考虑对问题进 ...

  7. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  8. Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp

    D. Two Paths 题目连接: http://codeforces.com/contest/14/problem/D Description As you know, Bob's brother ...

  9. Codeforces Beta Round #10 B. Cinema Cashier (树状数组)

    题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <ios ...

随机推荐

  1. Html5 Canvas 实现图片合成

    多个图片合成一张 <!doctype html> <html> <head> <meta charset="utf-8"> < ...

  2. 前端用node+mysql实现简单服务端

    node express + mysql实现简单服务端前端新人想写服务端不想学PHP等后端语言怎么办,那就用js写后台吧!这也是我这个前端新人的学习成果分享,如有那些地方不对,请给我指出. 1.准备工 ...

  3. 一种比使用协程更方便的方法:Invoke(),同样达到等待执行的效果

    1.Invoke(string methodName,float time) 在一定时间调用methodName函数 using UnityEngine; using System.Collectio ...

  4. 【Mac】gem install 出错 You don't have write permissions for the /Library/Ruby/Gems

    问题描述 RedisDump 是一个用于 Redis 数据导人/导出的工具,是基于 Ruby 实现的,需要先安装 Ruby.但因为 Mac 自带有 Ruby 所以我直接用gem install red ...

  5. 【 C 】高级字符串查找之查找标记(token)函数 strtok介绍

    我的csdn博客 一个字符串常常包含几个单独的部分,它们彼此被分隔开来.每次为了处理这些部分,你首先必须把它们从字符串中抽取出来. 这个任务有#include<string.h>中的str ...

  6. 20155233 《Java程序设计》实验四 Android开发基础

    20155233 <Java程序设计>实验四 Android开发基础 实验内容 1.基于Android Studio开发简单的Android应用并部署测试; 2.了解Android组件.布 ...

  7. Wiki版产品需求---产品需求文档到底是谁的?产品到底是谁的?

    在听了测试的一通唠叨之后,"内部实现一堆逻辑,只有一句话的需求文档","文档那么简单,我们怎么测试啊",心中突然想起来自己曾经干的一件当时觉得还不错的事情,但是 ...

  8. cdh中hdfs非ha环境迁移Namenode与secondaryNamenode,从uc机器到阿里;

    1.停掉外部接入服务: 2 NameNode Metadata备份: 2.1 备份fsimage数据,(该操作适用HA和非HA的NameNode),使用如下命令进行备份: [root@cdh01 df ...

  9. 欢迎使用 Flask¶

    欢迎使用 Flask¶ 欢迎阅读 Flask 文档. 本文档分为几个部分.我推荐您先从 安装 开始,之后再浏览 快速入门 章节. 教程 比快速入门更详细地介绍了如何用 Flask 创建一个完整的 应用 ...

  10. springboot入门之一:环境搭建

    springboot简介 springboot做为微服务的开发集合框架,有着天然的好处,它不像springmvc那样笨重繁杂,springmvc众多的配置使得开发人员很厌烦,为解决众多的配置带来的烦扰 ...