【状压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+opencv实现动态物体追踪
原文链接:https://blog.csdn.net/cike14/article/details/50649811 import cv2 import numpy as np camera=cv2. ...
- CSP201612-2:工资计算
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation
一.错误信息 Entity Framework 6.0数据迁移:Add-Migration XXXX 命令发生错误 System.Reflection.TargetInvocationExceptio ...
- 手动配置网卡配置文件ifcfg-eth0
linux 其他知识目录 原文链接:https://www.cnblogs.com/arvintang/p/5990599.html 网络接口配置文件[root@localhost ~]# cat / ...
- web登录密码加密
文章:如何实现登录页面密码加密 文章:用RSA加密实现Web登录密码加密传输 文章:web登录用户名密码加密 知乎文章:Web前端密码加密是否有意义? 文章:记录一次黑客模拟攻击 成功拿到淘宝账号和密 ...
- Java中的断言assert
Java陷阱之assert关键字 一.概述 在C和C++语言中都有assert关键,表示断言. 在Java中,同样也有assert关键字,表示断言,用法和含义都差不多. 二.语法 在J ...
- 主从复制redis
编辑主服务器的配置文件 注释下面一项 # slaveof 192.168.10.1 6379 主从复制 一主可以有多从,支持链式连级 一主多从 1:修改从服务器的配置文件/etc/redis.co ...
- 在原有的基础之上,启用NAT模型
# 给虚拟主机实例添加一个网关 route add default gw 192.168.23.1 # 在宿主机打开网卡间转发功能 echo 1 > /proc/sys/net/ipv4/i ...
- haproxy调度算法
调度算法详解 用balance指令指明调度算法, 例如:balance roundrobin 1:roundrobin :动态轮询算法,基于后端服务器的总权重做轮询,后端的服务器数量限制在4095 ...
- 常用的Redis客户端的并发模型(转)
伪代码模型 # get lock : timestamp = current Unix time + lock = SETNX lock.foo timestamp or (now() > ...