题目描述

房间里放着n块奶酪。一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处。

输入输出格式

输入格式:

第一行一个数n (n<=15)

接下来每行2个实数,表示第i块奶酪的坐标。

两点之间的距离公式=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))

输出格式:

一个数,表示要跑的最少距离,保留2位小数。

输入输出样例

输入样例#1:
复制

  1. 4
  2. 1 1
  3. 1 -1
  4. -1 1
  5. -1 -1
输出样例#1: 复制

  1. 7.41
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #include<cstdlib>
  5. #include<cstring>
  6. #include<string>
  7. #include<cmath>
  8. #include<map>
  9. #include<set>
  10. #include<vector>
  11. #include<queue>
  12. #include<bitset>
  13. #include<ctime>
  14. #include<time.h>
  15. #include<deque>
  16. #include<stack>
  17. #include<functional>
  18. #include<sstream>
  19. //#include<cctype>
  20. //#pragma GCC optimize(2)
  21. using namespace std;
  22. #define maxn 200005
  23. #define inf 0x7fffffff
  24. //#define INF 1e18
  25. #define rdint(x) scanf("%d",&x)
  26. #define rdllt(x) scanf("%lld",&x)
  27. #define rdult(x) scanf("%lu",&x)
  28. #define rdlf(x) scanf("%lf",&x)
  29. #define rdstr(x) scanf("%s",x)
  30. #define mclr(x,a) memset((x),a,sizeof(x))
  31. typedef long long ll;
  32. typedef unsigned long long ull;
  33. typedef unsigned int U;
  34. #define ms(x) memset((x),0,sizeof(x))
  35. const long long int mod = 1e9 + 7;
  36. #define Mod 1000000000
  37. #define sq(x) (x)*(x)
  38. #define eps 1e-5
  39. typedef pair<int, int> pii;
  40. #define pi acos(-1.0)
  41. //const int N = 1005;
  42. #define REP(i,n) for(int i=0;i<(n);i++)
  43. typedef pair<int, int> pii;
  44.  
  45. inline int rd() {
  46. int x = 0;
  47. char c = getchar();
  48. bool f = false;
  49. while (!isdigit(c)) {
  50. if (c == '-') f = true;
  51. c = getchar();
  52. }
  53. while (isdigit(c)) {
  54. x = (x << 1) + (x << 3) + (c ^ 48);
  55. c = getchar();
  56. }
  57. return f ? -x : x;
  58. }
  59.  
  60. ll gcd(ll a, ll b) {
  61. return b == 0 ? a : gcd(b, a%b);
  62. }
  63. int sqr(int x) { return x * x; }
  64.  
  65. /*ll ans;
  66. ll exgcd(ll a, ll b, ll &x, ll &y) {
  67. if (!b) {
  68. x = 1; y = 0; return a;
  69. }
  70. ans = exgcd(b, a%b, x, y);
  71. ll t = x; x = y; y = t - a / b * y;
  72. return ans;
  73. }
  74. */
  75.  
  76. int n;
  77. struct node {
  78. double x, y;
  79. }nd[20];
  80.  
  81. double dp[17][(1 << 16) + 2];
  82.  
  83. double dis(int i, int j) {
  84. 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));
  85. }
  86.  
  87. int main()
  88. {
  89. // ios::sync_with_stdio(0);
  90. n = rd(); ms(nd); mclr(dp, 127);
  91. for (int i = 1; i <= n; i++) {
  92. rdlf(nd[i].x); rdlf(nd[i].y);
  93. }
  94. for (int S = 0; S <= (1 << n) - 1; S++) {
  95. for (int i = 1; i <= n; i++) {
  96. if ((S&(1 << (i - 1))) == 0)continue;
  97. if (S == (1 << (i - 1))) {
  98. dp[i][S] = 0; continue;
  99. }
  100. for (int j = 1; j <= n; j++) {
  101. if (i == j)continue;
  102. if (S&(1 << (j - 1))) {
  103. dp[i][S] = min(dp[i][S], 1.0*dis(i, j) + dp[j][S - (1 << (i - 1))]);
  104. }
  105. }
  106. }
  107. }
  108. double MIN = 1.0*inf;
  109. for (int i = 1; i <= n; i++) {
  110. MIN = min(MIN, dis(0, i) + dp[i][(1 << n) - 1]);
  111. }
  112. printf("%.2lf\n", 1.0*MIN);
  113. return 0;
  114. }

吃奶酪 状压dp的更多相关文章

  1. 洛谷 P1433 吃奶酪 状压DP

    题目描述 分析 比较简单的状压DP 我们设\(f[i][j]\)为当前的状态为\(i\)且当前所在的位置为\(j\)时走过的最小距离 因为老鼠的坐标为\((0,0)\),所以我们要预处理出\(f[1& ...

  2. hihocoder #1608 : Jerry的奶酪(状压DP)

    传送门 题意 分析 设dp[i][j]为在i状态下当前在第j个奶酪的最小费用 转移方程:dp[(1<<k)|i][k]=dp[i][j]+d[j][k] 预处理出每个奶酪之间的距离,加入起 ...

  3. P1433 吃奶酪(洛谷)状压dp解法

    嗯?这题竟然是个绿题. 这个题真的不(很)难,我们只是不会计算2点之间的距离,他还给出了公式,这个就有点…… 我们直接套公式去求出需要的值,然后普通的状压dp就可以了. 是的状压dp. 这个题的数据加 ...

  4. [状压DP]吃奶酪

    吃 奶 酪 吃奶酪 吃奶酪 题目描述 房间里放着 n n n 块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在 ( 0 , 0 ) (0,0) (0,0)点处. 输入 第一行有一个整 ...

  5. hihocoder #1608 : Jerry的奶酪(状压dp)

    题目链接:http://hihocoder.com/problemset/problem/1608 题解:就是一道简单的状压dp由于dfs过程中只需要几个点之间的转移所以只要预处理一下几个点就行. # ...

  6. 状压DP之LGTB 与序列

    题目 思路 这道题竟然是状压DP,本人以为是数论,看都没看就去打下一题的暴力了,哭 \(A_i\)<=30,所以我们只需要考虑1-58个数,再往后选的话还不如选1更优,注意,1是可以重复选取的, ...

  7. 状压dp大总结1 [洛谷]

    前言 状态压缩是一种\(dp\)里的暴力,但是非常优秀,状态的转移,方程的转移和定义都是状压\(dp\)的难点,本人在次总结状压dp的几个题型和例题,便于自己以后理解分析状态和定义方式 状态压缩动态规 ...

  8. 状压DP复习笔记

    前言 复习笔记第4篇.CSP RP++. 引用部分为总结性内容. 0--P1433 吃奶酪 题目链接 luogu 题意 房间里放着 \(n\) 块奶酪,要把它们都吃掉,问至少要跑多少距离?一开始在 \ ...

  9. 有关状压DP

    [以下内容仅为本人在学习中的所感所想,本人水平有限目前尚处学习阶段,如有错误及不妥之处还请各位大佬指正,请谅解,谢谢!] 引言 动态规划虽然已经是对暴力算法的优化,但在某些比较特别的情况下,可以通过一 ...

随机推荐

  1. C++ 重载操作符- 02 重载输入输出操作符

    重载输入输出操作符 本篇博客主要介绍两个操作符重载.一个是 <<(输出操作符).一个是 >> (输入操作符) 现在就使用实例来学习:如何重载输入和输出操作符. #include ...

  2. ECS 游戏架构 应用

    转载自:http://blog.csdn.net/i_dovelemon/article/details/30250049 如何在cocos2d-x中使用ECS(实体-组件-系统)架构方法开发一个游戏 ...

  3. CentOS7.4配置SSH登录密码与密钥身份验证踩坑

    简单记录,自用CentOS7.4虚拟机与ALiYunVPS,在配置ssh登录身份验证时碰到的问题. 阿里云VPS:因为在重置磁盘时选择了密钥对的身份验证方式,因此VPS中的CentOS7.4中的 /e ...

  4. EXCEL 导入 R 的几种方法 R—readr和readxl包

    导入Excel数据至R语言的几种方法 如有如下Excel数据源,如何将数据导入R语言呢?今天主要来介绍几种常见的方法: 一.使用剪贴板,然后使用read.table函数: 首先选择Excel中的数据源 ...

  5. Exception (3) Java exception handling best practices

    List Never swallow the exception in catch block Declare the specific checked exceptions that your me ...

  6. javascript的caller,callee,call,apply[转]

    在提到上述的概念之前,首先想说说javascript中函数的隐含参数:arguments Arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments[ ...

  7. Ubuntu解压缩zip,tar,tar.gz,tar.bz2【转】

    ZIP zip可能是目前使用得最多的文档压缩格式.它最大的优点就是在不同的操作系统平台,比如Linux, Windows以及Mac OS,上使用.缺点就是支持的压缩率不是很高,而tar.gz和tar. ...

  8. Head First Python之4持久存储

    open()用法 # encoding:utf-8 try: # 写模式打开文件,若不存在该文件,则创建 out = open("data.out", "w") ...

  9. log4j.properties加入内容

    log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender. ...

  10. 11、Semantic-UI之分割线

    11.1 分割线的定义 示例:定义分割线 分割线 <div class="ui divider"></div> 竖线并加入or <div class= ...