[luoguP1433] 吃奶酪(DP || Dfs)
深搜加剪纸可A(O(玄学) 1274ms)
——代码
#include <cmath>
#include <cstdio>
#include <iostream> int n;
double ans = ~( << ), a[], b[];
bool vis[]; inline double min(double x, double y)
{
return x < y ? x : y;
} inline double query(int x, int y)
{
return sqrt((a[x] - a[y]) * (a[x] - a[y]) + (b[x] - b[y]) * (b[x] - b[y]));
} inline void dfs(int now, double sum, int k)
{
if(k == n + )
{
ans = min(ans, sum);
return;
}
if(sum > ans) return;
for(int i = ; i <= n; i++)
if(!vis[i])
{
vis[i] = ;
dfs(i, sum + query(now, i), k + );
vis[i] = ;
}
} int main()
{
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%lf %lf", &a[i], &b[i]);
dfs(, , );
printf("%.2lf\n", ans);
return ;
}
然而状压DP,稳一手(据说O(n2*2n) 73ms)
采用记忆化搜索。
设f[i][S]为已经走过的点的集合为S,当前停留在点i的最短距离
f[i][S] = f[j][S - i] + dis(i, j) (i, j ∈ S && i != j)
——代码
#include <cmath>
#include <cstdio> const int INF = 1e9;
int n;
double ans, a[], b[], f[][ << ]; inline double dist(int i, int j)
{
return sqrt((a[i] - a[j]) * (a[i] - a[j]) + (b[i] - b[j]) * (b[i] - b[j]));
} inline double min(double x, double y)
{
return x < y ? x : y;
} inline int istrue(int x, int S)
{
return ( << x - ) & S;
} inline int set0(int x, int S)
{
return (~( << x - )) & S;
} inline void dfs(int now, int S)
{
if(f[now][S] != -) return;
f[now][S] = INF;
for(int i = ; i <= n; i++)
{
if(i == now) continue;
if(!istrue(i, S)) continue;
dfs(i, set0(now, S));
f[now][S] = min(f[now][S], f[i][set0(now, S)] + dist(i, now));
}
} int main()
{
int i, j;
scanf("%d", &n);
for(i = ; i <= n; i++) scanf("%lf %lf", &a[i], &b[i]);
for(i = ; i <= n; i++)
for(j = ; j < ( << n); j++)
f[i][j] = -;
for(i = ; i <= n; i++) f[i][ << i - ] = dist(, i);
for(i = ; i <= n; i++) dfs(i, ( << n) - );
ans = INF;
for(i = ; i <= n; i++) ans = min(ans, f[i][( << n) - ]);
printf("%.2lf\n", ans);
return ;
}
用递推更简便(68ms)
——代码
#include <cmath>
#include <cstdio>
#include <cstring> const int INF = 1e9;
int n, m;
double ans, a[], b[], f[][ << ]; inline double dist(int i, int j)
{
return sqrt((a[i] - a[j]) * (a[i] - a[j]) + (b[i] - b[j]) * (b[i] - b[j]));
} inline double min(double x, double y)
{
return x < y ? x : y;
} int main()
{
int i, j, S, x, y;
scanf("%d", &n);
m = ( << n) - ;
for(i = ; i <= n; i++) scanf("%lf %lf", &a[i], &b[i]);
memset(f, 0x7f, sizeof(f));
for(i = ; i <= n; i++) f[i][ << i - ] = dist(, i);
for(S = ; S <= m; S++)
for(i = ; i <= n; i++)
{
if(!(( << i - ) & S)) continue;
for(j = ; j <= n; j++)
{
if(i == j) continue;
if(!(( << j - ) & S)) continue;
f[i][S] = min(f[i][S], f[j][( << i - ) ^ S] + dist(j, i));
}
}
ans = INF;
for(i = ; i <= n; i++) ans = min(ans, f[i][m]);
printf("%.2lf\n", ans);
return ;
}
[luoguP1433] 吃奶酪(DP || Dfs)的更多相关文章
- 洛谷 P1433 吃奶酪【DFS】+剪枝
题目链接:https://www.luogu.org/problemnew/show/P1433 题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处 ...
- 洛谷P1433 吃奶酪【dfs】【剪枝】
题目:https://www.luogu.org/problemnew/show/P1433 题意: 给定n个坐标,要求从(0,0)开始走遍所有点,最少经过的路程. 思路: 刚开始想像数字三角形一样适 ...
- [状压DP]吃奶酪
吃 奶 酪 吃奶酪 吃奶酪 题目描述 房间里放着 n n n 块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在 ( 0 , 0 ) (0,0) (0,0)点处. 输入 第一行有一个整 ...
- 解题报告:luogu P1433 吃奶酪
题目链接:P1433 吃奶酪 我感觉可以改成:[模板]TSP问题(商旅问题) 了. 爆搜\(T\)一个点,考虑状压\(dp\)(还是爆搜). 我们用\(dp[i][j]\)表示现在是\(i\)状态,站 ...
- 集训作业 洛谷P1433 吃奶酪
嗯?这题竟然是个绿题. 这个题真的不难,不要被他的难度吓到,我们只是不会计算2点之间的距离,他还给出了公式,这个就有点…… 我们直接套公式去求出需要的值,然后普通的搜索就可以了. 这个题我用的深搜,因 ...
- BZOJ-4424 &&CodeForces-19E Fairy DP+dfs (Link-Cut-Tree可A)
Va爷的胡策题T2 E. Fairy time limit per test1.5 seconds memory limit per test256 megabytes inputstandard i ...
- 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence
题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...
- HDU 4272 LianLianKan (状压DP+DFS)题解
思路: 用状压DP+DFS遍历查找是否可行.假设一个数为x,那么他最远可以消去的点为x+9,因为x+1~x+4都能被他前面的点消去,所以我们将2进制的范围设为2^10,用0表示已经消去,1表示没有消去 ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
随机推荐
- A brief preview of the new features introduced by OpenGL 3.3 and 4.0
A brief preview of the new features introduced by OpenGL 3.3 and 4.0 The Khronos Group continues t ...
- VUE中全局变量的定义和使用
目录 VUE中全局变量的定义和使用 1.工作中遇到的两类问题 1.1 状态值(标志) 1.2 传递字段 2.解决方法 2.1 VUEX 2.2 使用全局变量法管理状态与字段值 3.具体实现 3.1创建 ...
- 04-Vue中的动画
Vue中的动画 为什么要有动画:动画能够提高用户的体验,帮助用户更好的理解页面中的功能: -使用过渡类名 1.html <div id="app"> <input ...
- MySQL性能优化神器Explain
本文涉及:MySQL性能优化神器Explain的使用 简介 虽然使用Explain不能够马上调优我们的SQL,它也不能给予我们一些调整建议,但是它能够让我们了解MySQL 优化器是如何执行SQL 语句 ...
- 【题解】二逼平衡树 [P3380] [BZOJ3196] [Tyvj1730]
[题解]二逼平衡树 [P3380] [BZOJ3196] [Tyvj1730] 传送门:[模板]二逼平衡树(树套树)\([P3380]\) \([BZOJ3196]\) \([TYVJ1730]\) ...
- linux centos7 安装nginx并启动
Linux下安装Nginx完整教程及常见错误解决方案:https://blog.csdn.net/chenxiaochan/article/details/63688346 CentOS 7 安装Ng ...
- IDEA破解方法以及快捷键大全
IntelliJ IDEA2017.3 激活 - CSDN博客:https://blog.csdn.net/dc2222333/article/details/78582131 Eclipse和Int ...
- LN : leetcode 529 Minesweeper
lc 529 Minesweeper 529 Minesweeper Let's play the minesweeper game! You are given a 2D char matrix r ...
- android视频播放器系列(二)——VideoView
最近在学习视频相关的知识,现在也是在按部就班的一步步的来,如果有同样需求的同学可以跟着大家一起促进学习. 上一节说到了可以使用系统播放器以及浏览器播放本地以及网络视频,但是这在很大程度上并不能满足我们 ...
- Selenium学习第二天,了解Selenium工作模式与学习Selenium需要具备的知识与工具。
Selenium学习网站: 1.http://www.ltesting.net/ceshi/open/kygncsgj/selenium/2014/0408/207237.html——好像是对API的 ...