【状压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) ...
随机推荐
- Selenium自动化测试第二天(上)
如有任何学习问题,可以添加作者微信:lockingfree 目录 Selenium自动化测试基础 Selenium自动化测试第一天(上) Selenium自动化测试第一天(下) Selenium自动化 ...
- [转]操作系统Unix、Windows、Mac OS、Linux的故事
[写得很江湖气,可惜找不到原作者了] 文章转自:http://blog.csdn.net/wenmingchan/article/details/49925379 http://www.jb51.ne ...
- 那些年安装Appium遇到的坑
安装appium以及相关的总体记录 1 主要流程是参照这个来 https://www.cnblogs.com/wangyinghao/p/5780151.html 细节参考虫师的博客 http ...
- clean code(一)
代码整洁之道对于程序的重构及可读性至关重要.开始整洁之道吧!!! 一.抽离try catch 模块 public void delete(Page page){ try { deletePageAnd ...
- 完美的【去重留一】SQL
DELETE consum_record FROM consum_record, ( SELECT min(id) id, user_id, monetary, consume_time FROM c ...
- Friends and Cookies(思维)
Abood's birthday has come, and his n friends are aligned in a single line from 1 to n, waiting for t ...
- 【探路者】团队Alpha周贡献分数分配结果
经本组成员商议,根据老师提供的分数,(每人携带10分进入团队,[探路者]团队7人,共计35分). 本周每位同学携带10分进入组内,7人共计70分.分数公布如下: 吴雨丹 15分 贾雅杰 12分 蔺依铭 ...
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
Prime Distance Time Limit: 2 Seconds Memory Limit: 65536 KB The branch of mathematics called nu ...
- Codeforces Round #335 (Div. 2) D. Lazy Student 贪心+构造
题目链接: http://codeforces.com/contest/606/problem/D D. Lazy Student time limit per test2 secondsmemory ...
- springboot+vue+element:echarts开发遇见问题---后端sql(三)
<select id="getSumRequestRankingCount" parameterType="java.lang.String" resul ...