题面

看到数据范围那么小,一眼状压\(\text{DP}\)。

设\(dp[i][s]\)表示从\(i\)出发,走过的点的集合为\(s\)的最小距离。

不难推出转移方程(\(dis(i,j)\)为\(i\)点到\(j\)点的距离):

\[dp[i][s] = \min(dp[i][s], dp[j][s - (1 << (i - 1))] + dis(i,j))
\]

边界:

\[dp[i][1 << (i - 1)] = 0
\]

代码实现:

#include <bits/stdc++.h>
#define itn int
#define gI gi using namespace std; inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
} int n;
double x[20], y[20], dis[20][20], ans = 1000000000.0, dp[20][1 << 16]; inline double getjuli(double x, double y, double xx, double yy)
{
return sqrt((x - xx) * (x - xx) + (y - yy) * (y - yy));
} int main()
{
//freopen(".in", "r", stdin);
//freopen(".out", "w", stdout);
n = gi();
for (int i = 1; i <= n; i+=1) scanf("%lf %lf", &x[i], &y[i]);
for (int i = 1; i <= n; i+=1)
for (int j = 1; j <= n; j+=1)
dis[i][j] = getjuli(x[i], y[i], x[j], y[j]);//预处理出两点间的距离
memset(dp, 127, sizeof(dp));
for (int s = 1; s <= (1 << n) - 1; s+=1)
{
for (int i = 1; i <= n; i+=1)
{
if (!(s & (1 << (i - 1)))) continue;
if (s == (1 << (i - 1))) {dp[i][s] = 0; continue;}//边界
for (int j = 1; j <= n; j+=1)
{
if ((!s & (1 << (j - 1))) || i == j) continue;
dp[i][s] = min(dp[i][s], dp[j][s - (1 << (i - 1))] + dis[i][j]);//转移
}
}
}
for (int i = 1; i <= n; i+=1)
{
double ss = dp[i][(1 << n) - 1] + getjuli(0, 0, x[i], y[i]);//注意加上到点(0,0)的距离
if (ss < ans) ans = ss;
}
printf("%.2lf\n", ans);
return 0;
}

最新文章

  1. JAVA 1.8 理解面向对象程序设计
  2. 安装和使用Visual Studio 2013并进行简单的单元测试
  3. [No000030]程序员节发点别的:中国教育整个把人脑子搞坏了-易中天
  4. 详解 iOS 上机题!附个人见解
  5. Codeforce 287 div2 C题
  6. grep 常用参数详解
  7. Android快捷便利但不常被使用的原生工具类
  8. 一大早居然有骗子还是傻子,真是莫名其妙的,QQ1913522040,一看就是刚申请不久的
  9. hibernate二级缓存ehcache
  10. NOIP
  11. jQuery插件autoComplete使用详解
  12. [WCF]WCF起航
  13. C​# ​日​期​时​间
  14. 浅谈web应用的负载均衡、集群、高可用(HA)解决方案(转)
  15. vue移动端弹框组件,vue-layer-mobile
  16. 使用 webpack + react + redux + es6 开发组件化前端项目
  17. html5 Canvas绘制时钟以及绘制运动的圆
  18. 微信小程序获取复选框全选,反选选中的值
  19. MPU6050带字符驱动的i2c从设备驱动2
  20. SpringBoot2.X自定义拦截器实战及新旧配置对比(核心知识)

热门文章

  1. Serverless Component 介绍和使用指南
  2. Essential C++ 笔记-1
  3. linux---基础学习
  4. 纪中20日c组T2 2122. 【2016-12-31普及组模拟】幸运票
  5. 数据结构与算法 C++ 视频教程(4 套)百度网盘
  6. hdu6162
  7. JavaScirpt 一些基本知识
  8. Educational Codeforces Round 82 (Rated for Div. 2)
  9. [POI2013] LAN-Colorful Chain - 桶
  10. 【终端使用】常用Linux命令的基本使用