题目描述

房间里放着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<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std; map<int,double> m;
vector<int> G[];
vector<double> c[];
int N,vis[];
double ans=INF; struct cc{
double x,y;
}nod[]; double dis(int a,int b){
return sqrt((nod[a].x-nod[b].x)*(nod[a].x-nod[b].x)+(nod[a].y-nod[b].y)*(nod[a].y-nod[b].y));
} int look_up_table(double len){
long long H=;
for(int i=;i<=N;i++){
if(vis[i]) H=H*+i;
}
if(m.count(H)){
if(len>m[H]) return ;
m[H]=len;
return ;
}
else{
m[H]=len;
return ;
}
} void search(int x,int dep,double len){
if(len>=ans) return;
if(!look_up_table(len)) return;
if(dep==N){
ans=min(len,ans);
return;
}
vis[x]=;
// puts("$");
for(int i=;i<G[x].size();i++){
int now=G[x][i];
if(!vis[now]) search(now,dep+,len+c[x][i]);
} vis[x]=;
} int main(){
// freopen("01.in","r",stdin);
scanf("%d",&N);
for(int i=;i<=N;i++){
cin>>nod[i].x>>nod[i].y;
//scanf("%lf",&nod[i].x,&nod[i].y);
}
nod[].x=nod[].y=0.0;
for(int i=;i<=N;i++){
for(int j=i+;j<=N;j++){
G[i].push_back(j);
c[i].push_back(dis(i,j)); G[j].push_back(i);
c[j].push_back(dis(i,j));
}
}
search(,,0.0);
printf("%.2lf",ans);
return ;
}

90分 的map>_<

//以上为map,意料之外啊

hash貌似不可以,因为自己的数据都过不了,就没有提交

至于评测的时候我觉得直接STL就好,hash试一试吧

看了下题解,我竟无言以对

回溯以及剪枝。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std; int N,vis[];
double ans=INF; struct cc{
double x,y;
}nod[]; double dis(int a,int b){
return sqrt((nod[a].x-nod[b].x)*(nod[a].x-nod[b].x)+(nod[a].y-nod[b].y)*(nod[a].y-nod[b].y));
} void search(int x,int dep,double len){
if(len>=ans) return;
if(dep==N){
ans=min(len,ans);
return;
} for(int i=;i<=N;i++){
if(i==x)continue;
if(!vis[i]){
vis[i]=;
search(i,dep+,len+dis(x,i));
vis[i]=;
}
} } int main(){
// freopen("01.in","r",stdin);
scanf("%d",&N);
for(int i=;i<=N;i++)cin>>nod[i].x>>nod[i].y;
search(,,0.0);
printf("%.2lf",ans);
return ;
}

完了?

行吧,电脑渣13个点的数据用暴力都跑不出来怪谁咯

洛谷 P1433 吃奶酪 Label:dfs && 剪枝Ex的更多相关文章

  1. 洛谷 P1433 吃奶酪【DFS】+剪枝

    题目链接:https://www.luogu.org/problemnew/show/P1433 题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处 ...

  2. 洛谷P1433 吃奶酪【dfs】【剪枝】

    题目:https://www.luogu.org/problemnew/show/P1433 题意: 给定n个坐标,要求从(0,0)开始走遍所有点,最少经过的路程. 思路: 刚开始想像数字三角形一样适 ...

  3. 集训作业 洛谷P1433 吃奶酪

    嗯?这题竟然是个绿题. 这个题真的不难,不要被他的难度吓到,我们只是不会计算2点之间的距离,他还给出了公式,这个就有点…… 我们直接套公式去求出需要的值,然后普通的搜索就可以了. 这个题我用的深搜,因 ...

  4. 洛谷 - P1433 - 吃奶酪 - dfs

    https://www.luogu.org/problemnew/show/P1433 并不是每一个求最短距离就是bfs,这个肯定是dfs. 直接计算15!可以知道枚举必定超时,但是! 我们dfs非常 ...

  5. 洛谷 P1433 吃奶酪(记忆化)

    题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...

  6. 洛谷 P1433 吃奶酪

    题目描述 房间里放着n块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在(0,0)点处. 输入输出格式 输入格式: 第一行一个数n (n<=15) 接下来每行2个实数,表示第i块 ...

  7. 洛谷P1433 吃奶酪 题解 状态压缩DP

    题目链接:https://www.luogu.com.cn/problem/P1433 题目大意 房间里放着 \(n\) 块奶酪.一只小老鼠要把它们都吃掉,问至少要跑多少距离?老鼠一开始在 \((0, ...

  8. 洛谷P1433 吃奶酪

    #include<iostream> #include<math.h> using namespace std ; ; int n; bool st[N]; double x[ ...

  9. 洛谷 P1433 吃奶酪 状压DP

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

随机推荐

  1. ASP.NET MVC使用Bundle来打包压缩js和css

    Bundle它是用来将js和css进行压缩(多个文件可以打包成一个文件),并且可以区分调试和非调试,在调试时不进行压缩,以原始方式显示出来,以方便查找问题. 1.BundleConfig配置Bundl ...

  2. 【转载】python super的用法

    转载地址: http://blog.csdn.net/cxm19830125/article/details/20610533 super的用法是调用继承类的初始化方法,如下面的代码: class A ...

  3. thinkphp自动验证中的静态验证和动态验证和批量验证

    1.静态定义 在模型类里面预先定义好该模型的自动验证规则,我们称为静态定义. 举例说明,我们在模型类里面定义了$_validate属性如下: class UserModel extends Model ...

  4. 第四篇:SOUI资源文件组织

    什么是资源? 现代的软件只要有UI,基本上少不了资源. 资源是什么?资源就是在程序运行时提供固定的数据源的文件. 在MFC当道的时代,资源一般就是位图(Bitmap),图标(Icon),光标(Curs ...

  5. ios github网址

    ios github网址 http://github.ibireme.com/github/list/ios/

  6. ios录音

    #import "ViewController.h" #import <AVFoundation/AVFoundation.h> @interface ViewCont ...

  7. 内核函数KiFastCallEntry

    KiFastCallEntry() 机制分析 概述 Win32 子系统 API 调用 ntdll!ZwWriteFile() 函数 ntdll!KiFastSystemCall() 函数 _KUSER ...

  8. php echo return exit 区别

    echo.print().printf().sprintf().vardump().varexport():都可以输出内容到网页,但不退出函数或程序. return:返回并立即退出,函数级别. die ...

  9. Arduino101学习笔记(六)—— 高级IO

    1.位移输出函数(8位) 输入value数据后Arduino会自动把数据移动分配到8个并行输出端. 其中dataPin为连接DS的引脚号, clockPin为连接SH_CP的引脚号, bitOrder ...

  10. Understanding Kafka Consumer Groups and Consumer Lag

    In this post, we will dive into the consumer side of this application ecosystem, which means looking ...