传送门

Description

给你n个点,每次可以从起点到最多两个点然后回到起点。求经过每个点最少一次的最短欧氏距离和是多少

Input

第一行是起点的坐标

第二行是点的个数\(n\)

下面\(n\)行是需要进过的点的坐标

Output

输出最短欧氏距离以及方案。方案是经过每个点的顺序。起点为\(0\)号点

Hint

\(For~All:\)

\(0~\leq~n~\leq~24\)

Solution

看到24就大概能想到是个状压DP

考虑做法

设\(f_S\)为走遍\(S\)中的点的ans。

转移任意枚举两个或一个点转移

然而这么做是\(O(4^n)\)的,GG

考虑事实上对于同一个状态,比如走过前3个点,第一次走1,2,第二次走3和第一次走3,第二次走1,2的答案是一样的。

于是对于一个集合,只任意选择集合中的一个元素,枚举他是怎么选的,就可以得到最优的答案。

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#define rg register
#define ci const int
#define cl const long long int typedef long long int ll; namespace IO {
char buf[300];
} template <typename T>
inline void qr(T &x) {
rg char ch=getchar(),lst=' ';
while((ch > '9') || (ch < '0')) lst=ch,ch=getchar();
while((ch >= '0') && (ch <= '9')) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
if(lst == '-') x=-x;
} template <typename T>
inline void qw(T x,const char aft,const bool pt) {
if(x < 0) {putchar('-');x=-x;}
rg int top=0;
do {
IO::buf[++top]=x%10+'0';
} while(x/=10);
while(top) putchar(IO::buf[top--]);
if(pt) putchar(aft);
} template <typename T>
inline T mmax(const T a,const T b) {return a > b ? a : b;}
template <typename T>
inline T mmin(const T a,const T b) {return a < b ? a : b;}
template <typename T>
inline T mabs(const T a) {return a < 0 ? -a : a;} template <typename T>
inline void mswap(T &a,T &b) {
T _temp=a;a=b;b=_temp;
} const int maxn = 30;
const int maxt = 20000000; struct M {
int p,v;
};
//M list[maxt]; struct Pos {
int x,y;
};
Pos MU[maxn]; int sx,sy,n,tcnt;
int frog[maxt],pre[maxt],list[maxt]; void dfs(ci);
int cost(ci,ci);
int dist(ci,ci); int main() {
qr(sx);qr(sy);qr(n);int dn=n-1;
MU[n].x=sx;MU[n].y=sy;
for(rg int i=0;i<n;++i) {qr(MU[i].x);qr(MU[i].y);}
for(rg int i=0;i<dn;++i) {
for(rg int j=i+1;j<n;++j) {
int p=(1<<i)|(1<<j);
int v=cost(i,j);
list[p]=v;
}
}
for(rg int i=0;i<n;++i) {
int p=1<<i;int v=dist(n,i)<<1;list[p]=v;
}
int all=(1<<n)-1;
memset(frog,0x3f,sizeof frog);frog[0]=0;
for(rg int i=1;i<=all;++i) {
for(rg int j=0;j<n;++j) if(i&(1<<j)) {
for(rg int k=0;k<n;++k) if(i&(1<<k)) {
int p=(1<<j)|(1<<k);
if(frog[i] > (frog[i^p]+list[p])) frog[i]=frog[i^p]+list[p],pre[i]=p;
}
break;
}
}
qw(frog[all],'\n',true);
dfs(all);
return 0;
} inline int cost(ci a,ci b) {
return dist(n,a)+dist(a,b)+dist(b,n);
} inline int dist(ci a,ci b) {
return (MU[a].x-MU[b].x)*(MU[a].x-MU[b].x)+(MU[a].y-MU[b].y)*(MU[a].y-MU[b].y);
} void dfs(ci x) {
if(!x) {qw(0,' ',true);return;}
dfs(x^pre[x]);
for(rg int i=0;i<n;++i) if(pre[x]&(1<<i)) qw(i+1,' ',true);
qw(0,' ',true);
}

Summary

当多个状态的转移等价的时候,考虑只枚举其中一个状态。

【状压DP】【CF8C】 Looking for Order的更多相关文章

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

    题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...

  2. codeforces 8C. Looking for Order 状压dp

    题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...

  3. 【题解】codeforces 8c Looking for Order 状压dp

    题目描述 Lena喜欢秩序井然的生活.一天,她要去上大学了.突然,她发现整个房间乱糟糟的--她的手提包里的物品都散落在了地上.她想把所有的物品都放回她的手提包.但是,这里有一点问题:她一次最多只能拿两 ...

  4. ZOJ3802 Easy 2048 Again (状压DP)

    ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  5. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

  6. HDUOJ Clear All of Them I 状压DP

    Clear All of Them I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 122768/62768 K (Java/Oth ...

  7. HDU 1074 Doing Homework【状压DP】

    Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...

  8. Doing Homework HDU - 1074 (状压dp)

    Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every ...

  9. 【状压DP】【HDOJ1074】

    http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (Java/Others) ...

随机推荐

  1. TW实习日记:前三天

    今天是2018年7月20号,周五.从周一开始实习到现在,终于想起来要写日记这种东西了,可以记录一下自己这一天所学所做所知也是蛮不错的.先简单总结一下自己的大学生活吧,算是多姿多彩,体验了很多东西.在大 ...

  2. 获取label标签内for的属性值-js

    <body> <div class="row_2" id="ass"> <label for="aaa"> ...

  3. Centos7与Centos6的区别

    CentOS7 修改网卡名称为eth0.eth1 方法1 修改网卡名称 cd /etc/sysconfig/network-scripts/ mv ifcfg-eno16777736 ifcfg-et ...

  4. eclipse技巧-快捷键

    ctrl + 1,快速修复 ctrl + d, 快捷删除行 shift + Enter,快速移动光标到下一行 ctrl + F11,运行代码 alt + ↑/↓,快速移动行 ctrl + alt + ...

  5. python3【基础】-list&tuple

    一.list概述 list (列表)是python中最常用的数据类型之一,通过列表可以对数据实现最方便的存储,修改等操作.在python3中,list支持如下方法: Help on class lis ...

  6. linux下svn操作(专)

    原文地址:https://www.cnblogs.com/clicli/p/5913330.html svn命令在linux下的使用SVN软件版本管理 1.将文件checkout到本地目录svn ch ...

  7. 20172330 2017-2018-1 《Java程序设计》第十一周学习总结

    20172330 2017-2018-1 <程序设计与数据结构>第十一周学习总结 教材学习内容总结 本周的学习内容为集合 Android简介 Android操作系统是一种多用户的Linux ...

  8. 软工1816 · Alpha冲刺(9/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 学习jQuery的AJAX部分的基础知识,对web端如何异步获取服务器信息有了 ...

  9. 细节--服务器mysql空密码

    在部署致服务器的时候 发现mysql密码为空的情况 如果采用 root账户的话 试过很多 比如不写下面这行 <property name="password" value=& ...

  10. 处理了一个以前写的java小程序的异常

    之前用java做过0-99的数字和英文之间的翻译,输入数字就会翻译成英文,输入英文会翻译成数字,比如输入56  输出fiftysix   输入fiftysix  输出56, 发现这会有一个异常,当输入 ...