【状压DP】【CF8C】 Looking for Order
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的更多相关文章
- 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 ...
- codeforces 8C. Looking for Order 状压dp
题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...
- 【题解】codeforces 8c Looking for Order 状压dp
题目描述 Lena喜欢秩序井然的生活.一天,她要去上大学了.突然,她发现整个房间乱糟糟的--她的手提包里的物品都散落在了地上.她想把所有的物品都放回她的手提包.但是,这里有一点问题:她一次最多只能拿两 ...
- ZOJ3802 Easy 2048 Again (状压DP)
ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...
- 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 ...
- 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 ...
- HDU 1074 Doing Homework【状压DP】
Doing Homework Problem Description Ignatius has just come back school from the 30th ACM/ICPC. Now he ...
- 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 ...
- 【状压DP】【HDOJ1074】
http://acm.hdu.edu.cn/showproblem.php?pid=1074 Doing Homework Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- Python接口测试实战2 - 使用Python发送请求
如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...
- ddms+adt+jdk的安装及调试开发安卓
_______ ddms+adt+jdk的安装及调试开发安卓 目录 阐述 1 1 jdk安装 1 2 sdk安装 3 3 Eclipse安装 6 4 ADT安装 10 5 Ddms使用 16 ...
- appium+python自动化☞环境搭建
前言:appium可以说是做app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本语言也是支持java和Python.略懂Python,所以接下来的教程是 appium+pyt ...
- [转]Git 撤销操作
二. Git撤消操作 12.1 修改最后一次提交 git commit --amend 1.新建一个文件 2.提交一个之前的更改 3.跟踪这个文件 4.跟前一次一起提交 提示你是否重新编辑提交说明,如 ...
- 算法与AI的暗黑面:3星|《算法的陷阱:超级平台、算法垄断与场景欺骗》
算法的陷阱:超级平台.算法垄断与场景欺骗 全书讲算法与AI的暗黑面:价格歧视.导致算法军备竞赛.导致商家降价冲动降低.平台作恶(向劣质商家收费导致品质下降.与开发商一起分析用户隐私)等. 作者从商业. ...
- 利用box-shadow制作loading图
我们见过很多利用css3做的loading图,像下面这种应该是很常见的.通常制作这种loading,我们会一个标签对应一个圆,八个圆就要八个标签.但是这种做法很浪费资源.我们可以只用一个标签,然后利用 ...
- Knight Moves(广搜BFS)
Description A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to ...
- 第一个scrum会议
第一阶段冲刺任务认领: PM薛哥: 让手电筒亮起来 梁哥: 代码测试 康哥: 用户反馈等等
- BETA事后总结
目录 所有成员 项目宣传视频链接 贡献比例 工作流程 组员分工 本组 Beta 冲刺站立会议博客链接汇总 燃尽图 原计划.达成情况及原因分析 组员:胡绪佩 组员:周政演 组员:庄卉 组员:何家伟 组员 ...
- Ubuntu命令行安装显卡驱动
1. sudo apt-et purge nvidia* 卸载原有驱动 2. sudo add-apt-repository ppa:graphics-drivers sudo apt-get upd ...