双调欧几里得旅行商问题(TSPhdu2224)
http://acm.hdu.edu.cn/showproblem.php?pid=2224
The shortest path
Before you reach the rightmost point Pn, you can only visit the points those have the bigger x-coordinate value. For example, you are at Pi now, then you can only visit Pj(j > i). When you reach Pn, the rule is changed, from now on you can only visit the points
those have the smaller x-coordinate value than the point you are in now, for example, you are at Pi now, then you can only visit Pj(j < i). And in the end you back to P1 and the tour is over.
You should visit all points in this tour and you can visit every point only once.
the coordinate of the i-th point in the plane.
1 1
2 3
3 1
Hint: The way 1 - 3 - 2 - 1 makes the shortest path.
题意:平面上n个点,确定一条连接各点的最短闭合旅程。这个解的一般形式为NP的(在多项式时间内可以求出)
建议通过只考虑双调旅程(bitonictour)来简化问题,这种旅程即为从最左点开始,严格地从左到右直至最右点,然后严格地从右到左直至出发点。每个点都要走一次,且每个点只能走一次,求最短路径;
设dp[i][j]代表起始点到i的距离+起始点到j的距离,中间没有交叉点,且没有遗漏点(dp[i][j]=dp[j][i]);
当i<j-1的时候,dp[i][j]是从dp[i][j-1]传递过去的,即dp[i][j]=dp[i][j-1]+dis[j-1][j];
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include"stdio.h"
#include"string.h"
#include"iostream"
#include"map"
#include"string"
#include"queue"
#include"stdlib.h"
#include"algorithm"
#include"math.h"
#define M 222
#define eps 1e-10
#define inf 100000000000000
#define mod 1000000000
#define INF 1000000000
using namespace std;
struct node
{
double x,y;
}p[M];
double dp[M][M],dis[M][M];
double min(double a,double b)
{
return a<b?a:b;
}
double Len(node a,node b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
int n,i,j;
while(scanf("%d",&n)!=-1)
{
for(i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
sort(p,p+n,cmp);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
dis[i][j]=Len(p[i],p[j]);
dp[0][1]=dis[0][1];
for(j=2;j<n;j++)
{
for(i=0;i<j-1;i++)
dp[i][j]=dp[i][j-1]+dis[j-1][j];
dp[j-1][j]=inf;
for(i=0;i<j-1;i++)
dp[j-1][j]=min(dp[j-1][j],dp[i][j-1]+dis[i][j]);
}
dp[n-1][n-1]=dp[n-2][n-1]+dis[n-2][n-1];
printf("%.2lf\n",dp[n-1][n-1]);
}
}
双调欧几里得旅行商问题(TSPhdu2224)的更多相关文章
- 2014年百度之星程序设计大赛 - 资格赛 1002 Disk Schedule(双调欧几里得旅行商问题)
Problem Description 有非常多从磁盘读取数据的需求,包含顺序读取.随机读取.为了提高效率,须要人为安排磁盘读取.然而,在现实中,这样的做法非常复杂.我们考虑一个相对简单的场景.磁盘有 ...
- 2014百度之星第二题Disk Schedule(双调欧几里得旅行商问题+DP)
Disk Schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- POJ2677 Tour(DP+双调欧几里得旅行商问题)
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3929 Accepted: 1761 Description ...
- hdu 2224 双调欧几里得旅行商问题tsp
/* 题意:平面上n个点,确定一条连接各点的最短闭合旅程且每个点仅用一次.这个解的一般形式为NP的(在多项式时间内可以求出) 建议通过只考虑双调旅程(bitonictour)来简化问题,这种旅程即为从 ...
- 欧几里得旅行商问题 java与c++实现
双调欧几里得旅行商问题是一个经典动态规划问题.<算法导论(第二版)>思考题15-1 旅行商问题描述:平面上n个点,确定一条连接各点的最短闭合旅程.这个解的一般形式为NP的(在多项式时间内可 ...
- 【HDU2224】The shortest path(双调欧几里得dp)
算法导论上一道dp,挺有趣的.于是就研究了一阵. dp(i, j)代表从左边第一个点到第i个点与从从左边最后一个点(即为第一个点)到j点的最优距离和.于是找到了子状态. 决策过程 dp[i][j] = ...
- NOIP2012拓展欧几里得
拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)
http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...
- poj 1061 青蛙的约会 拓展欧几里得模板
// poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...
随机推荐
- MATLAB中常用的排列、组合、阶乘函数
1.求n的阶乘,方法如下:a.factorial(n)b.gamma(n+1)c.v='n!'; vpa(v) 2.求组合(数),方法如下:a.combntns(x,m) 列举出从n个元素中取出 ...
- 总结一发linux常用命令
显示目录和文件的命令 Ls:用于查看所有文件夹的命令. Dir:用于显示指定文件夹和目录的命令 Tree: 以树状图列出目录内容 Du:显示目录或文件大小 修改目录,文件权限和属主及数组命令 Ch ...
- 嵌入式驱动开发之usb 无线网卡驱动---RT2870STA dm368
RT2870STA 368的无线网卡驱动! http://www.linuxidc.com/Linux/2014-02/96979.htm
- HDU 1020:Encoding
pid=1020">Encoding Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- u3d change terrain textrue&height
using UnityEngine; using System.Collections; public class terrainTest : MonoBehaviour { ; private Te ...
- openal 基础知识3
四创新科技extension (Creative Labs'Extensions) 创新科技为OpenAL添加了多个extensions,许多都利用了他们声卡的特性. “Enumerate All”e ...
- openstack中nova组件Hypervisors、Floating_ips的全部python API 汇总
感谢朋友支持本博客,欢迎共同探讨交流,因为能力和时间有限.错误之处在所难免,欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- hbase shell学习-2
一个学生成绩表的例子来演示hbase的用法. name grade course math english Tom 5 97 87 Jim 4 89 80 表的创建:语法:create '表名称',' ...
- asp.net DropDownList实现二级联动效果
1.在aspx页面中,拖入两个DroDownList控件,代码如下: <div> <asp:DropDownList ID="s1" runat=" ...
- day05<Java语言基础--数组>
Java语言基础(数组概述和定义格式说明) Java语言基础(数组的初始化动态初始化) Java语言基础(Java中的内存分配以及栈和堆的区别) Java语言基础(数组的内存图解1一个数组) Java ...