洛谷P1433 吃奶酪【dfs】【剪枝】
题目:https://www.luogu.org/problemnew/show/P1433
题意:
给定n个坐标,要求从(0,0)开始走遍所有点,最少经过的路程。
思路:
刚开始想像数字三角形一样适用next_permutation,枚举坐标的顺序,一旦出现距离比当前最优解要差时就sort剪枝。
这里sort的起始和结束要注意一下,和那道题不一样。开始应该是i+1
但是还是有一个点会TLE。毕竟sort了一下还是会慢一点...?
所以还是老老实实dfs吧。道理都是一样的,搜索走的下标排列,一旦出现比最优解差就直接回溯。
#include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue> #define inf 0x7f7f7f7f
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; int n;
struct node{
double x, y;
}pos[];
int per[];
double d[][]; double dist(node a, node b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
} bool cmp(int a, int b)
{
return a > b;
} bool vis[];
double ans = inf;
void dfs(int pre, int m, double res)
{
if(res > ans)return;
if(m == n){
ans = min(ans, res);
return;
}
for(int i = ; i <= n; i++){
if(!vis[i]){
vis[i] = true;
dfs(i, m + , res + d[pre][i]);
vis[i] = false;
}
}
return;
} int main()
{
scanf("%d", &n);
pos[].x = ;
pos[].y = ;
// node st;
// st.x = 0;st.y = 0;
for(int i = ; i <= n; i++){
scanf("%lf%lf", &pos[i].x, &pos[i].y);
per[i] = i;
}
for(int i = ; i <= n; i++){
for(int j = i + ; j <= n; j++){
d[i][j] = d[j][i] = dist(pos[i], pos[j]);
}
} dfs(, , ); // double ans = inf;
// do{
// double tmp = 0;
// //double tmp = dist(st, pos[per[1]]);
// for(int i = 1; i < n; i++){
// tmp += d[per[i]][per[i + 1]];
// //tmp += dist(pos[per[i]], pos[per[i + 1]]);
// if(tmp > ans){
// sort(per + i + 1, per + 1 + n, cmp);
// break;
// }
// }
//// for(int i = 1; i <= n; i++){
//// cout<<per[i]<<" ";
//// }
//// cout<<endl;
//// if(tmp < ans){
////
//// }
// //tmp += dist(st, pos[per[1]]);
// tmp += d[0][per[1]];
// ans = min(ans, tmp);
// }while(next_permutation(per + 1, per + 1 + n)); printf("%.2lf\n", ans);
return ;
}
洛谷P1433 吃奶酪【dfs】【剪枝】的更多相关文章
- 洛谷 - P1433 - 吃奶酪 - dfs
https://www.luogu.org/problemnew/show/P1433 并不是每一个求最短距离就是bfs,这个肯定是dfs. 直接计算15!可以知道枚举必定超时,但是! 我们dfs非常 ...
- 洛谷 P1433 吃奶酪【DFS】+剪枝
题目链接:https://www.luogu.org/problemnew/show/P1433 题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处 ...
- 洛谷 P1433 吃奶酪 Label:dfs && 剪枝Ex
题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...
- 洛谷 P1433 吃奶酪(记忆化)
题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...
- 集训作业 洛谷P1433 吃奶酪
嗯?这题竟然是个绿题. 这个题真的不难,不要被他的难度吓到,我们只是不会计算2点之间的距离,他还给出了公式,这个就有点…… 我们直接套公式去求出需要的值,然后普通的搜索就可以了. 这个题我用的深搜,因 ...
- 洛谷 P1433 吃奶酪
题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...
- 洛谷P1433 吃奶酪 题解 状态压缩DP
题目链接:https://www.luogu.com.cn/problem/P1433 题目大意 房间里放着 \(n\) 块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在 \((0, ...
- 洛谷P1433 吃奶酪
#include<iostream> #include<math.h> using namespace std ; ; int n; bool st[N]; double x[ ...
- 洛谷 P1433 吃奶酪 状压DP
题目描述 分析 比较简单的状压DP 我们设\(f[i][j]\)为当前的状态为\(i\)且当前所在的位置为\(j\)时走过的最小距离 因为老鼠的坐标为\((0,0)\),所以我们要预处理出\(f[1& ...
随机推荐
- C# System.Collections.Stack
using System; using System.Collections; public class SamplesStack { public static void Main() { // C ...
- mysql insert锁机制
https://blog.csdn.net/zhanghongzheng3213/article/details/53436240
- 让div获取焦点
DIV获取焦点有两种方法: tabindex="0" contenteditable="true" ①:设置div为可编辑状态,则可点击获取焦点,同时div的内 ...
- Springmvc 整合 jetbrick 实例
应用环境: <jetbrick.version>1.2.8</jetbrick.version> <antlr4-runtime.version>4.2.2< ...
- mysqlcheck与myisamchk的区别
mysqlcheck和myisamchk都可以用来检测和修复表.(主要是MyISAM表)但是也有以下不同点:1.都可以检查.分析和修复myisam表.但是mysqlcheck也可以检查.分析innod ...
- Zabbix-2.X/3.X监控工具监控Redis以及zabbix Redis监控模板下载
为了监控Redis3的运行状况,去zabbix官网查找资料,根据提示,找到了这个项目:https://github.com/blacked/zbx_redis_template 但是文档和内容已经不匹 ...
- HTML5 完美解决javascript中iphone手机和android手机复制文本到剪切板问题
1.执行以下解决方案条件:(这个是原理) ①执行复制方法时 所复制文字不能被任何 块级元素和行内块元素和行内元素遮盖否则无效:(解决方案:将文本通过绝对定位或其他方式移除屏幕外) ②ios中不能复制属 ...
- 一道简单的python面试题-购物车
要求实现:1.程序开始运行时要求手动填入工资金额2.然后展示一份带有价格的商品列表3.选择某个商品,足够金额购买就添加到购物车,否则提示无法购买4.退出后列出购物车清单 #!/usr/bin/pyth ...
- CSA Enterprise Architecture图
https://research.cloudsecurityalliance.org/tci/index.php/explore/
- vue子父组件的通信
Element使用的是Vue2.0版本,众所周知在Vue 1.0升级到2.0中去除了$broadcast和$dispatch方法. 1.父组件向子组件传值 a.app.vue父组件 <templ ...