题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2224

题意:平面上有n个点,问去的路只能从左到右,回的路只能从右到左的,且来回必须经过所有点的最小路径;

dp[i][j] 表示以j为起点,1为拐点 ,i为终点的最短路;

j < i-1 时,那么i-1这个点在来的路径上 必然等于dp[i-1][j] + dis[i-1][i] ;

j = i -1 ,那么i-1这个点在回的路径上,那么dp[i][j] = min(dp[k][j] + dis[k][j]) 1<=k < j; 因为dp[i][j] = dp[j][i], 所以dp[i][j] = min(dp[j][k] + dis[k][j]) 1<=k < j

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <algorithm>
  5. #include <iostream>
  6. #include <time.h>
  7. #include <vector>
  8. #include <queue>
  9.  
  10. typedef long long LL;
  11.  
  12. using namespace std;
  13.  
  14. const int N = ;
  15. const double eps = 1e-;
  16. const int INF = 0x3f3f3f3f;
  17. const int mod = ;
  18. const double PI = *atan(1.0);
  19.  
  20. struct point
  21. {
  22. double x, y;
  23. }p[N];
  24.  
  25. double Dist(point p1, point p2)
  26. {
  27. return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
  28. }
  29.  
  30. int n;
  31. double dp[N][N];///表示以j为起点,1为拐点,i为终点,经历所有i到j之间的点;
  32. /*
  33. double DP()
  34. {
  35. dp[1][1] = 0;
  36. dp[2][1] = Dist(p[1], p[2]);
  37. for(int i=3; i<=n; i++)
  38. {
  39. for(int j=1; j<i-1; j++)
  40. dp[i][j] = dp[i-1][j] + Dist(p[i-1],p[i]);
  41. double Min = INF;
  42. for(int k=1; k<i-1; k++)
  43. Min = min(Min, dp[i-1][k] + Dist(p[k], p[i]));
  44. dp[i][i-1] = Min;
  45. }
  46. dp[n][n] = dp[n][n-1] + Dist(p[n-1], p[n]);
  47. return dp[n][n];
  48. }*/
  49.  
  50. double DFS(int s, int e)
  51. {
  52. if(dp[s][e] != )
  53. return dp[s][e];
  54. if(s < e-)
  55. dp[s][e] = DFS(s, e-) + Dist(p[e-], p[e]);
  56. else
  57. {
  58. double Min = INF;
  59. for(int i=; i<e-; i++)
  60. Min = min(Min, DFS(i, s) + Dist(p[i], p[e]));
  61. dp[s][e] = Min;
  62. }
  63. return dp[s][e];
  64. }
  65.  
  66. int main()
  67. {
  68. while(scanf("%d", &n) != EOF)
  69. {
  70. memset(dp, , sizeof(dp));
  71. for(int i=; i<=n; i++)
  72. scanf("%lf %lf", &p[i].x, &p[i].y);
  73. ///double ans = DP();
  74. dp[][] = ;
  75. dp[][] = dp[][] = Dist(p[], p[]);
  76. DFS(n-, n);
  77. dp[n][n] = dp[n-][n] + Dist(p[n-], p[n]);
  78. printf("%.2f\n", dp[n][n]);
  79. }
  80. return ;
  81. }

The shortest path---hdu2224 && Tour---poj2677(旅行商问题)的更多相关文章

  1. HDU 2224 The shortest path

    The shortest path Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. hdu-----(2807)The Shortest Path(矩阵+Floyd)

    The Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  3. zoj 2760 How Many Shortest Path 最大流

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...

  4. The Shortest Path in Nya Graph

    Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...

  5. hdu 3631 Shortest Path(Floyd)

    题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...

  6. Shortest Path(思维,dfs)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  7. Shortest Path

    Shortest Path Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。

    Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...

  9. 【ZOJ2760】How Many Shortest Path

    How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...

  10. [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes

    An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...

随机推荐

  1. 51nod1256(乘法逆元)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1256 题意:中文题诶~ 思路: M, N 互质, 求满足 K ...

  2. HBASE列族不能太多的真相 (一个table有几个列族就有几个 Store)

    HRegionServer内部管理了一系列HRegion对象,每个HRegion对 应了table中的一个region,HRegion中由多 个HStore组成.每个HStore对应了Table中的一 ...

  3. c#去掉小数点后的无效0

    decimal d = 0.0500m; d.ToString("0.##")就出来了 也可以这样 string.Format("{0:0.##}",d000) ...

  4. Light oj1031 Easy Game (区间dp)

    题目链接:http://vjudge.net/contest/140891#problem/F A和B都足够聪明,只有我傻,想了好久才把代码和题意对应上[大哭] 代码: #include<ios ...

  5. Linux 安装tomcat

    安装命令: yum install tomcat 安装完后默认: 安装路径: /usr/share/tomcat 执行命令(启动,停止, 重启): /etc/rc.d/init.d/tomcat 配置 ...

  6. iOS 设置1像素的UIView线

    如果是代码实现,直接 在CGRectMake里把对应的参数设置为: 1.0/[UIScreenmainScreen].scale 即可.         如果是用xib实现,就需要将对应的限制拖一个I ...

  7. Python for Infomatics 第12章 网络编程六(译)

    注:文章原文为Dr. Charles Severance 的 <Python for Informatics>.文中代码用3.4版改写,并在本机测试通过. 12.9 词汇表 Beautif ...

  8. mallmold开源商城系统网银在线chinabank支付插件

    最近没事捣鼓项目,找了个轻型商城系统mallmold,用起来还觉的挺不错的,尤其是mallmold中文版,赞一个.中文版集成了大部分主流支付系统,但因是个人网站,没法获得对应的服务,最终选择了网银在线 ...

  9. 下载php扩展笔记

    查找相关php的扩展网址https://pecl.php.net/index.php PECL 的全称是 The PHP Extension Community Library ,即PHP 扩展库.是 ...

  10. cxf webservice简单应用

    Server端 server部署到一个端口号为80的tomcat中 CXFController.java package com.lwj.controller; import java.io.IOEx ...