题目链接: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

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <time.h>
#include <vector>
#include <queue> typedef long long LL; using namespace std; const int N = ;
const double eps = 1e-;
const int INF = 0x3f3f3f3f;
const int mod = ;
const double PI = *atan(1.0); struct point
{
double x, y;
}p[N]; double Dist(point p1, point p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
} int n;
double dp[N][N];///表示以j为起点,1为拐点,i为终点,经历所有i到j之间的点;
/*
double DP()
{
dp[1][1] = 0;
dp[2][1] = Dist(p[1], p[2]);
for(int i=3; i<=n; i++)
{
for(int j=1; j<i-1; j++)
dp[i][j] = dp[i-1][j] + Dist(p[i-1],p[i]);
double Min = INF;
for(int k=1; k<i-1; k++)
Min = min(Min, dp[i-1][k] + Dist(p[k], p[i]));
dp[i][i-1] = Min;
}
dp[n][n] = dp[n][n-1] + Dist(p[n-1], p[n]);
return dp[n][n];
}*/ double DFS(int s, int e)
{
if(dp[s][e] != )
return dp[s][e];
if(s < e-)
dp[s][e] = DFS(s, e-) + Dist(p[e-], p[e]);
else
{
double Min = INF;
for(int i=; i<e-; i++)
Min = min(Min, DFS(i, s) + Dist(p[i], p[e]));
dp[s][e] = Min;
}
return dp[s][e];
} int main()
{
while(scanf("%d", &n) != EOF)
{
memset(dp, , sizeof(dp));
for(int i=; i<=n; i++)
scanf("%lf %lf", &p[i].x, &p[i].y);
///double ans = DP();
dp[][] = ;
dp[][] = dp[][] = Dist(p[], p[]);
DFS(n-, n);
dp[n][n] = dp[n-][n] + Dist(p[n-], p[n]);
printf("%.2f\n", dp[n][n]);
}
return ;
}

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. 你真的了解iOS代理设计模式吗?

    在项目中我们经常会用到代理的设计模式,这是iOS中一种消息传递的方式,也可以通过这种方式来传递一些参数.这篇文章会涵盖代理的使用技巧和原理,以及代理的内存管理等方面的知识.我会通过这些方面的知识,带大 ...

  2. iOS之九宫格图片

    照片 现在人们的生活越来越丰富了,很多美好的瞬间都定格在一张张色彩绚丽的照片上,或许把照片珍藏在相册里,或许通过社交软件分享给亲朋好友.那社交软件上的照片是以什么形式展现的呢?那么接下来就要说到九宫格 ...

  3. PHP中删除数组空值的方法

    array_filter函数的功能是利用回调函数来对数组进行过滤,如果没有回调函数,那么默认就是删除数组中值为false的项目. 例如 $entry = array(                0 ...

  4. Python for Data Analysis

    Data Analysis with Python ch02 一些有趣的数据分析结果 Male描述的是美国新生儿男孩纸的名字的最后一个字母的分布 Female描述的是美国新生儿女孩纸的名字的最后一个字 ...

  5. delphi.memory.分配及释放---New/Dispose, GetMem/FreeMem及其它函数的区别与相同

    我估摸着内存分配+释放是个基础函数,有些人可能没注意此类函数或细究,但我觉得还是弄明白的好. 介绍下面内存函数前,先说一下MM的一些过程,如不关心可忽略: TMemoryManager = recor ...

  6. CentOS7安装Oracle 11gR2 安装

    概述 Oracle 在Linux和window上的安装不太一样,公司又是Linux系统上的Oracle,实在没辙,研究下Linux下Oracle的使用,oracle默认不支持CentOS系统安装,所以 ...

  7. kNN算法python实现和简单数字识别

    kNN算法 算法优缺点: 优点:精度高.对异常值不敏感.无输入数据假定 缺点:时间复杂度和空间复杂度都很高 适用数据范围:数值型和标称型 算法的思路: KNN算法(全称K最近邻算法),算法的思想很简单 ...

  8. uva12546. LCM Pair Sum

    uva12546. LCM Pair Sum One of your friends desperately needs your help. He is working with a secret ...

  9. 2016 Multi-University Training Contest 1 I. Solid Dominoes Tilings

    Solid Dominoes Tilings Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  10. 自动提交Git branch代码评审到Review Board系统

    背景 敏捷软件开发中,越小的反馈环,意味着软件质量越容易得到保证. 作为组件团队,我们的开发任务中,往往存在一些特性涉及到几十个功能点,开发周期持续数周或数月的情况.如何在开发过程中保证软件质量,是个 ...