题目:

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);
}

最新文章

  1. Mysql Error: Host ‘xxx.xxx.xxx.xxx’ is not allowed to connect to
  2. python sproto支持64位有符号整数
  3. 在Swift中整数以及浮点的格式化
  4. knockout之各种数据绑定方法:text、attr、visible、html、css、style绑定
  5. MyBatis学习系列二——增删改查
  6. 安卓Design包之TabLayout控件的使用
  7. UIImagePickerController显示中文界面
  8. Object-C @synthesize -- 笔记
  9. C#的输入输出及基本类型
  10. mysql timestamp 值不合法问题
  11. 《Android进阶》之第四篇 ViewPagerIndicator的使用
  12. django全文检索
  13. jmeter(二十六)生成HTML性能测试报告
  14. bzoj3122 [SDOI2013]随机数生成器
  15. SpringBoot图片上传(五) 上一篇的新版本,样式修改后的
  16. Vue2全家桶之一:vue-cli(vue脚手架)超详细教程
  17. Centos7系统如何不重启系统识别新添加的硬盘?
  18. P3760 [TJOI2017]异或和
  19. Spring注解之@Lazy注解
  20. python 关闭垃圾回收

热门文章

  1. OSS文档1
  2. ESA2GJK1DH1K升级篇: 快速的移植升级程序到自己的项目(APP用户程序制作)
  3. Xamarin.Forms移动开发系列3:项目剖析
  4. NLP中一些数学知识
  5. [LeetCode] 723. Candy Crush 糖果消消乐
  6. [LeetCode] 296. Best Meeting Point 最佳开会地点
  7. oracle--PMON
  8. Elasticsearch由浅入深(八)搜索引擎:mapping、精确匹配与全文搜索、分词器、mapping总结
  9. Object.setPrototypeOf() 与Object.getPrototypeOf() 方法的使用
  10. 分布式数据库缓存系统Apache Ignite