吃奶酪 状压dp
题目描述
房间里放着n块奶酪。一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处。
输入输出格式
输入格式:
第一行一个数n (n<=15)
接下来每行2个实数,表示第i块奶酪的坐标。
两点之间的距离公式=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
输出格式:
一个数,表示要跑的最少距离,保留2位小数。
输入输出样例
- 7.41
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- #include<cstdlib>
- #include<cstring>
- #include<string>
- #include<cmath>
- #include<map>
- #include<set>
- #include<vector>
- #include<queue>
- #include<bitset>
- #include<ctime>
- #include<time.h>
- #include<deque>
- #include<stack>
- #include<functional>
- #include<sstream>
- //#include<cctype>
- //#pragma GCC optimize(2)
- using namespace std;
- #define maxn 200005
- #define inf 0x7fffffff
- //#define INF 1e18
- #define rdint(x) scanf("%d",&x)
- #define rdllt(x) scanf("%lld",&x)
- #define rdult(x) scanf("%lu",&x)
- #define rdlf(x) scanf("%lf",&x)
- #define rdstr(x) scanf("%s",x)
- #define mclr(x,a) memset((x),a,sizeof(x))
- typedef long long ll;
- typedef unsigned long long ull;
- typedef unsigned int U;
- #define ms(x) memset((x),0,sizeof(x))
- const long long int mod = 1e9 + 7;
- #define Mod 1000000000
- #define sq(x) (x)*(x)
- #define eps 1e-5
- typedef pair<int, int> pii;
- #define pi acos(-1.0)
- //const int N = 1005;
- #define REP(i,n) for(int i=0;i<(n);i++)
- typedef pair<int, int> pii;
- inline int rd() {
- int x = 0;
- char c = getchar();
- bool f = false;
- while (!isdigit(c)) {
- if (c == '-') f = true;
- c = getchar();
- }
- while (isdigit(c)) {
- x = (x << 1) + (x << 3) + (c ^ 48);
- c = getchar();
- }
- return f ? -x : x;
- }
- ll gcd(ll a, ll b) {
- return b == 0 ? a : gcd(b, a%b);
- }
- int sqr(int x) { return x * x; }
- /*ll ans;
- ll exgcd(ll a, ll b, ll &x, ll &y) {
- if (!b) {
- x = 1; y = 0; return a;
- }
- ans = exgcd(b, a%b, x, y);
- ll t = x; x = y; y = t - a / b * y;
- return ans;
- }
- */
- int n;
- struct node {
- double x, y;
- }nd[20];
- double dp[17][(1 << 16) + 2];
- double dis(int i, int j) {
- return 1.0*sqrt((nd[i].x - nd[j].x)*(nd[i].x - nd[j].x) + (nd[i].y - nd[j].y)*(nd[i].y - nd[j].y));
- }
- int main()
- {
- // ios::sync_with_stdio(0);
- n = rd(); ms(nd); mclr(dp, 127);
- for (int i = 1; i <= n; i++) {
- rdlf(nd[i].x); rdlf(nd[i].y);
- }
- for (int S = 0; S <= (1 << n) - 1; S++) {
- for (int i = 1; i <= n; i++) {
- if ((S&(1 << (i - 1))) == 0)continue;
- if (S == (1 << (i - 1))) {
- dp[i][S] = 0; continue;
- }
- for (int j = 1; j <= n; j++) {
- if (i == j)continue;
- if (S&(1 << (j - 1))) {
- dp[i][S] = min(dp[i][S], 1.0*dis(i, j) + dp[j][S - (1 << (i - 1))]);
- }
- }
- }
- }
- double MIN = 1.0*inf;
- for (int i = 1; i <= n; i++) {
- MIN = min(MIN, dis(0, i) + dp[i][(1 << n) - 1]);
- }
- printf("%.2lf\n", 1.0*MIN);
- return 0;
- }
吃奶酪 状压dp的更多相关文章
- 洛谷 P1433 吃奶酪 状压DP
题目描述 分析 比较简单的状压DP 我们设\(f[i][j]\)为当前的状态为\(i\)且当前所在的位置为\(j\)时走过的最小距离 因为老鼠的坐标为\((0,0)\),所以我们要预处理出\(f[1& ...
- hihocoder #1608 : Jerry的奶酪(状压DP)
传送门 题意 分析 设dp[i][j]为在i状态下当前在第j个奶酪的最小费用 转移方程:dp[(1<<k)|i][k]=dp[i][j]+d[j][k] 预处理出每个奶酪之间的距离,加入起 ...
- P1433 吃奶酪(洛谷)状压dp解法
嗯?这题竟然是个绿题. 这个题真的不(很)难,我们只是不会计算2点之间的距离,他还给出了公式,这个就有点…… 我们直接套公式去求出需要的值,然后普通的状压dp就可以了. 是的状压dp. 这个题的数据加 ...
- [状压DP]吃奶酪
吃 奶 酪 吃奶酪 吃奶酪 题目描述 房间里放着 n n n 块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在 ( 0 , 0 ) (0,0) (0,0)点处. 输入 第一行有一个整 ...
- hihocoder #1608 : Jerry的奶酪(状压dp)
题目链接:http://hihocoder.com/problemset/problem/1608 题解:就是一道简单的状压dp由于dfs过程中只需要几个点之间的转移所以只要预处理一下几个点就行. # ...
- 状压DP之LGTB 与序列
题目 思路 这道题竟然是状压DP,本人以为是数论,看都没看就去打下一题的暴力了,哭 \(A_i\)<=30,所以我们只需要考虑1-58个数,再往后选的话还不如选1更优,注意,1是可以重复选取的, ...
- 状压dp大总结1 [洛谷]
前言 状态压缩是一种\(dp\)里的暴力,但是非常优秀,状态的转移,方程的转移和定义都是状压\(dp\)的难点,本人在次总结状压dp的几个题型和例题,便于自己以后理解分析状态和定义方式 状态压缩动态规 ...
- 状压DP复习笔记
前言 复习笔记第4篇.CSP RP++. 引用部分为总结性内容. 0--P1433 吃奶酪 题目链接 luogu 题意 房间里放着 \(n\) 块奶酪,要把它们都吃掉,问至少要跑多少距离?一开始在 \ ...
- 有关状压DP
[以下内容仅为本人在学习中的所感所想,本人水平有限目前尚处学习阶段,如有错误及不妥之处还请各位大佬指正,请谅解,谢谢!] 引言 动态规划虽然已经是对暴力算法的优化,但在某些比较特别的情况下,可以通过一 ...
随机推荐
- C++ 重载操作符- 02 重载输入输出操作符
重载输入输出操作符 本篇博客主要介绍两个操作符重载.一个是 <<(输出操作符).一个是 >> (输入操作符) 现在就使用实例来学习:如何重载输入和输出操作符. #include ...
- ECS 游戏架构 应用
转载自:http://blog.csdn.net/i_dovelemon/article/details/30250049 如何在cocos2d-x中使用ECS(实体-组件-系统)架构方法开发一个游戏 ...
- CentOS7.4配置SSH登录密码与密钥身份验证踩坑
简单记录,自用CentOS7.4虚拟机与ALiYunVPS,在配置ssh登录身份验证时碰到的问题. 阿里云VPS:因为在重置磁盘时选择了密钥对的身份验证方式,因此VPS中的CentOS7.4中的 /e ...
- EXCEL 导入 R 的几种方法 R—readr和readxl包
导入Excel数据至R语言的几种方法 如有如下Excel数据源,如何将数据导入R语言呢?今天主要来介绍几种常见的方法: 一.使用剪贴板,然后使用read.table函数: 首先选择Excel中的数据源 ...
- Exception (3) Java exception handling best practices
List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...
- javascript的caller,callee,call,apply[转]
在提到上述的概念之前,首先想说说javascript中函数的隐含参数:arguments Arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[ ...
- Ubuntu解压缩zip,tar,tar.gz,tar.bz2【转】
ZIP zip可能是目前使用得最多的文档压缩格式.它最大的优点就是在不同的操作系统平台,比如Linux, Windows以及Mac OS,上使用.缺点就是支持的压缩率不是很高,而tar.gz和tar. ...
- Head First Python之4持久存储
open()用法 # encoding:utf-8 try: # 写模式打开文件,若不存在该文件,则创建 out = open("data.out", "w") ...
- log4j.properties加入内容
log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender. ...
- 11、Semantic-UI之分割线
11.1 分割线的定义 示例:定义分割线 分割线 <div class="ui divider"></div> 竖线并加入or <div class= ...