题目描述

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

输入输出格式

输入格式:

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

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

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

输出格式:

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

输入输出样例

输入样例#1:
复制

4
1 1
1 -1
-1 1
-1 -1
输出样例#1: 复制

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的更多相关文章

  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. SpringBoot中使用AOP实现计算Service执行时间

    1.增加POM.XML的依赖架包 <!-- 引入 spring aop 依赖 --><dependency> <groupId>org.springframewor ...

  2. RTX二次开发笔记1

    在客户端,一个rtx用户给另一个rtx用户发送消息! 我的解决方案:在rtx服务端部署一个 wcf服务 或 webservice 服务,然后程序在客户端调用服务就行. 1,C#版 (服务端需要4个DL ...

  3. python3--json序列化

    # Auther: Aaron Fan # 把数据存入到一个文件中 # json格式的数据几乎可以通用语任何编程语言,但是仅仅只是简单的格式转换# 比如:字典.列表.元组.字符串这些,像函数.类就不可 ...

  4. Python3 urllib.parse 常用函数示例

    Python3 urllib.parse 常用函数示例 http://blog.51cto.com/walkerqt/1766670  1.获取url参数. >>> from url ...

  5. LoadRunner出现error问题及解决方法总结

    一.Step download timeout (120 seconds) 这是一个经常会遇到的问题,解决得办法走以下步骤:1.   修改run time setting中的请求超时时间,增加到600 ...

  6. bootstrap实现去点列表、内联列表、水平定义列表

    内联列表:通过添加类名“.list-inline”来实现内联列表,简单点说就是把垂直列表换成水平列表,而且去掉项目符号(编号),保持水平显示. 去点列表:通过给无序列表添加一个类名“.list-uns ...

  7. 【转载】如何选择MySQL存储引擎

    一.MySQL的存储引擎 完整的引擎说明还是看官方文档:http://dev.mysql.com/doc/refman/5.6/en/storage-engines.html 这里介绍一些主要的引擎 ...

  8. 37 有n个人围成一圈,顺序排号,从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号那位.

    题目:有n个人围成一圈,顺序排号,从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号那位. public class _037NumberOff { public st ...

  9. javascript总结40:DOM中操作样式的两种方式

    1 DOM中操作样式的两种方式 1 通过元素的style属性 注意: 通过style属性设置样式时,css中要写单位的属性,在js代码中也要加单位 //html <div id="bo ...

  10. “undefined reference to JNI_GetCreatedJavaVM”和“File format not recognized”错误原因分析

    "undefined reference to JNI_GetCreatedJavaVM"和"File format not recognized"错误原因分析 ...