双调欧几里得旅行商问题(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 ...
随机推荐
- easui Pagination Layout
分页显示方式有几种 layout: ['first', 'prev', 'next', 'last'] layout: ['list', 'sep', 'first', 'prev', 'sep', ...
- R绘制中国地图,并展示流行病学数据
流行病学的数据讲究“三间分布”,即人群分布.时间分布和空间分布.其中的“空间分布”最好是在地图上展示,才比较清楚.R软件集统计分析与高级绘图于大成,是最适合做这项工作了.关于地图的绘制过程,谢益辉.邱 ...
- 45本免费的JavaScript书籍资源收集
JavaScript目前变得越来越流行,已经变成了Web开发必备的语言,加之其跨平台的特性,使得在一切皆为JavaScript的移动互联网时代大有作为. 同时,我们看到,在过去的这一年的软件开发中,J ...
- C# 窗体显示避免抢夺焦点
通过调用API进行显示可以避免抢夺焦点的问题 以下是API调用 using System.Runtime.InteropServices; [DllImport("user32.dll&qu ...
- Hello World! 这是我的第一个 CGI 程序
Hello World! 这是我的第一个 CGI 程序上面的 C++ 程序是一个简单的程序,把它的输出写在 STDOUT 文件上,即显示在屏幕上.在这里,值得注意一点,第一行输出 Content-ty ...
- Unity使用JsonFX插件进行序列化
孙广东 2015.6.25 Unity and JSON – Quick Guide: 相比較XML的沉重和密集,Json更加高效. Introduction: 什么是 Json ?假设你从未使用过 ...
- unity3d中的DontDestroyOnLoad来回切换出现多个实例问题
在用Unity3D开发游戏中,我们会经常创建多个场景,但是在场景过度的时候,通常场景中的对象会被删除.所以Unity3D给了我们一个不删除前一个 场景中的某一个对象或者脚本的API,那就是“DontD ...
- 接口日志记录AOP实现-LogAspect
使用spring aop日志记录 所需jar包 pom.xml <!-- logger begin --> <dependency> <groupId>org.sl ...
- 【ML】scikit-learn-book
http://nbviewer.ipython.org/github/gmonce/scikit-learn-book/tree/master/
- <img/>标签onerror事件在IE下的bug和解决方法
IE下打开网页时,会弹出“Stack overflow at line: 0”的弹框.经分析,这个bug是由于img标签的onerror事件引起的.程序中用到的代码片段如下:正常情况下显示src所指路 ...