洛谷P1523 旅行商简化版(DP)
2024-08-25 02:50:42
题目:
P1523 旅行商简化版
解析
可以看做是两个人同时从西往东走,经过不一样的点,走到最东头的方案数
设\(f[i][j]\)表示一个人走到i,一个人走到j的最短距离(\(i<j\))
第\(j+1\)个位置,两个人都可能走,两种情况
- \(f[i][j+1]=min\{f[i][j+1],f[i][j]+dis[j][j+1]\}\)位置在j的人走到了j+1位置
- \(f[j][j+1]=min\{f[j][j+1],f[i][j]+dis[i][j+1]\}\)位置在i的人走到了j+1位置
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m;
double f[N][N], dis[N][N];
struct node {
double x, y;
bool operator <(const node &oth) const {
return x < oth.x;
}
} e[N];
double calc(node a, node b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
int main() {
ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; ++i)
cin >> e[i].x >> e[i].y;
sort(e + 1, e + 1 + n);
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
dis[i][j] = calc(e[i], e[j]), f[i][j] = LLONG_MAX;
f[1][2] = dis[1][2];
for (int i = 1; i <= n; ++i)
for (int j = i + 1; j <= n; ++j) {
f[i][j + 1] = min(f[i][j + 1], f[i][j] + dis[j][j + 1]);
f[j][j + 1] = min(f[j][j + 1], f[i][j] + dis[i][j + 1]);
}
double ans = LLONG_MAX;
for (int i = 1; i < n; ++i) ans = min(ans, f[i][n] + dis[i][n]);
printf("%.2lf", ans);
}
最新文章
- Mysql Error: Host ‘xxx.xxx.xxx.xxx’ is not allowed to connect to
- python sproto支持64位有符号整数
- 在Swift中整数以及浮点的格式化
- knockout之各种数据绑定方法:text、attr、visible、html、css、style绑定
- MyBatis学习系列二——增删改查
- 安卓Design包之TabLayout控件的使用
- UIImagePickerController显示中文界面
- Object-C @synthesize -- 笔记
- C#的输入输出及基本类型
- mysql timestamp 值不合法问题
- 《Android进阶》之第四篇 ViewPagerIndicator的使用
- django全文检索
- jmeter(二十六)生成HTML性能测试报告
- bzoj3122 [SDOI2013]随机数生成器
- SpringBoot图片上传(五) 上一篇的新版本,样式修改后的
- Vue2全家桶之一:vue-cli(vue脚手架)超详细教程
- Centos7系统如何不重启系统识别新添加的硬盘?
- P3760 [TJOI2017]异或和
- Spring注解之@Lazy注解
- python 关闭垃圾回收
热门文章
- OSS文档1
- ESA2GJK1DH1K升级篇: 快速的移植升级程序到自己的项目(APP用户程序制作)
- Xamarin.Forms移动开发系列3:项目剖析
- NLP中一些数学知识
- [LeetCode] 723. Candy Crush 糖果消消乐
- [LeetCode] 296. Best Meeting Point 最佳开会地点
- oracle--PMON
- Elasticsearch由浅入深(八)搜索引擎:mapping、精确匹配与全文搜索、分词器、mapping总结
- Object.setPrototypeOf() 与Object.getPrototypeOf() 方法的使用
- 分布式数据库缓存系统Apache Ignite