题目链接: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 - 使用自定义字体-苹方字体

    苹方提供了六个字重,font-family 定义如下:苹方-简 常规体font-family: PingFangSC-Regular, sans-serif;苹方-简 极细体font-family: ...

  2. php 总结第一篇(望大家补充!谢谢)

    /* 数组的常用函数 * * 数组的排序函数 *   sort() *   rsort() *   usort() *   asort() *   arsort() *   uasort() *   ...

  3. [IDEA] 快捷键学习

    IntelliJ Idea 常用快捷键列表   Alt+回车 导入包,自动修正Ctrl+N   查找类Ctrl+Shift+N 查找文件Ctrl+Alt+L  格式化代码 Ctrl+Alt+O 优化导 ...

  4. 作弊Q-百威

    ===_=374793763===_= 2652880032 865580818 大康 2652880032 春牛 3479301404  皮卡丘 3242026908  舍得放手

  5. dedecms 按权重排序不准或BUG的处理方法

    dede:list 的方法 1.找到"根目录\include\arc.listview.class.php"文件. 2.修改代码:在文件第727行处添加按weight排序判断代码( ...

  6. 【原】iOS学习之控制器的创建

    本次博客是一篇总结性质的博客,总结的是各种创建控制器的方式以及一些需要注意的操作. 1.通过storyboard创建控制器 正如我上一篇博客中所说,当 Main Interface 没有选定的时候,我 ...

  7. DOM元素的大小和位置

    HTML: <div id="parent"> <div id="box"> 测试测试测试测试测试测试测试测试测试测试测试测试测试测试测 ...

  8. HDU 2243 考研路茫茫——单词情结(AC自动机+DP+快速幂)

    题目链接 错的上头了... 这题是DNA的加强版,26^1 +26^2... - A^1-A^2... 先去学了矩阵的等比数列求和,学的是第二种方法,扩大矩阵的方法.剩下就是各种模板,各种套. #in ...

  9. ZeroMQ接口函数之 :zmq_disconnect - 断开一个socket的连接

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_disconnect zmq_disconnect(3) ØMQ Manual - ØMQ/3.2.5 Name ...

  10. php获取html纯文本,解决编辑器手动键入空格造成的无意义空白字符(空值问题)

    在项目中,我们常常需要用到一些验证,不管是前台还是后台的,上传的问题时,需要内容不为空,但可视化编辑器的介入让手动敲入空格跳出了常规的检测.空格是一种排版的手段,但毫无内容只有空格就显得没有意义了,今 ...