The shortest path---hdu2224 && Tour---poj2677(旅行商问题)
题目链接: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(旅行商问题)的更多相关文章
- HDU 2224 The shortest path
The shortest path Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Shortest Path
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (中等) 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 ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
- [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 ...
随机推荐
- Client病毒已感染超7万人 暗扣费并频弹广告
恶意木马病毒横行,您的钱包还hold得住吗?猎豹移动安全实验室与安天AVL移动安全团队于2015年下半年,共同截获一款名为Client的木马病毒,并且对该病毒进行持续监测.通过进一步关注,我们发现该病 ...
- Power BI for Office 365(三)Power Pivot
在Power Pivot中可以从各种数据源中根据你的需求来创建数据模型,并且可以根据需要随时刷新这些数据.在上一篇中,Anna已经准备好了加载到Power Pivot中的数据.Power Pivot就 ...
- Expression: is_block_type_valid(header->block_use)
VS2015 用 openmesh read_mesh 读取网格时,这样一段代码 void CPathFace::test2() { string file = ".\\data\\fa ...
- linux 自编软件运行时权限不足问题
在非根目录下" ./*.out " 文件时出现显示运行的权限不足的问题,经过调查,有人给出方案: 方案1: 在Linux下执行一个文件时候提示“权限不够”的解决办法如下 转到那个文 ...
- Log4net用法
日记是我们在程序中经常用到的,故记于此 首先要下载Log4net.dll 官方网站:http://logging.apache.org/log4net/ vs里创建一个c#控制台程序,在App.con ...
- [USACO] 铺放矩形块 题解
题目大意: 给定4个矩形块,找出一个最小的封闭矩形将这4个矩形块放入,但不得相互重叠.所谓最小矩形指该矩形面积最小. 思路: 枚举矩形的安放顺序,再按照题目所给的图判断即可,主要要想到枚举. 代码: ...
- HDU 1556 线段树或树状数组,插段求点
1.HDU 1556 Color the ball 区间更新,单点查询 2.题意:n个气球,每次给(a,b)区间的气球涂一次色,问最后每个气球各涂了几次. (1)树状数组 总结:树状数组是一个查 ...
- ajax详细介绍
a.什么是Ajax Asynchronous JavaScript and XML(异步JavaScript和XML) 节省用户操作,时间,提高用户体验,减少数据请求 传输获取数据 ...
- php经典算法(转载)
//-------------------- // 基本数据结构算法 //-------------------- //二分查找(数组里查找某个元素) function bin_sch($arr ...
- Django入门1
Django中的应用是以APP形式存在的,这是Django的主要特色. 入门--Hello,Django 在mysite中新建文件views.py,并在里面定义方法hello: 这里#-*- ...