poj2677 Tour
题意:
双调欧几里得旅行商问题。
思路:
dp。定义dp[i][j](i <= j)为从点j从右向左严格按照x坐标递减顺序走到点1,之后再从点1从左向右严格按照x坐标递增的顺序走到点i,并且在此过程中经过且仅经过1到j之间所有的点1次。则
dp[1][2] = dis[1][2];
dp[i][j] = dp[i][j - 1] + dis[j - 1][j]; (i < j - 1)
dp[i][j] = min(dp[k][j - 1] + dis[k][j]). (1 <= k < j - 1, i == j - 1)
最终,dp[n][n] = dp[n - 1][n] + dis[n - 1][n].
实现:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std; const int MAXN = , INF = 0x3f3f3f3f;
double dp[MAXN + ][MAXN + ], dis[MAXN + ][MAXN + ];
int n, x[MAXN + ], y[MAXN + ]; int square(int x)
{
return x * x;
} void init()
{
for (int i = ; i <= n; i++)
{
for (int j = i + ; j <= n; j++)
{
dis[i][j] = sqrt(square(x[i] - x[j]) + square(y[i] - y[j]));
}
}
} double solve()
{
dp[][] = dis[][];
for (int j = ; j <= n; j++)
{
// i < j - 1
for (int i = ; i <= j - ; i++)
{
dp[i][j] = dp[i][j - ] + dis[j - ][j];
}
// i == j - 1
dp[j - ][j] = INF;
for (int k = ; k <= j - ; k++)
{
dp[j - ][j] = min(dp[j - ][j], dp[k][j - ] + dis[k][j]);
}
}
return dp[n][n] = dp[n - ][n] + dis[n - ][n];
} int main()
{
while (cin >> n)
{
for (int i = ; i <= n; i++)
{
cin >> x[i] >> y[i];
}
init();
printf("%.2f\n", solve());
}
return ;
}
总结:
假设一个最优选择,然后再基于该最优选择来定义问题,是动态规划的惯用手法。
poj2677 Tour的更多相关文章
- POJ2677 Tour[DP 状态规定]
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4307 Accepted: 1894 Description ...
- POJ2677 Tour(DP+双调欧几里得旅行商问题)
Tour Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3929 Accepted: 1761 Description ...
- POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9276 Accepted: 3924 ...
- Euler Tour Tree与dynamic connectivity
Euler Tour Tree最大的优点就是可以方便的维护子树信息,这点LCT是做不到的.为什么要维护子树信息呢..?我们可以用来做fully dynamic connectivity(online) ...
- soj 1015 Jill's Tour Paths 解题报告
题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...
- poj1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8859 Accepted: 3728 ...
- A quick tour of JSON libraries in Scala
A quick tour of JSON libraries in Scala Update (18.11.2015): added spray-json-shapeless libraryUpdat ...
- POJ 1637 Sightseeing tour (混合图欧拉路判定)
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6986 Accepted: 2901 ...
- POJ 1637 Sightseeing tour (混合图欧拉回路)
Sightseeing tour Description The city executive board in Lund wants to construct a sightseeing tou ...
随机推荐
- FileReader、 FileWriter、readLine()和newLine()、LineNumberReader(二十一)
1.字符流FileReader * 1.字符流是什么 * 字符流是可以直接读写字符的IO流 * 字符流读取字符, 就要先读取到字节数据, 然后转为字符. 如果要写出字符, 需要把字符转为字节再写出. ...
- hdu-2680 Choose the best route(最短路)
题目链接: Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- hdu-3592 World Exhibition(差分约束)
题目链接: World Exhibition Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- 关于将word转化为pdf 文件调用jacob 包
用jacob. 先到官方网站上去下载:http://sourceforge.net/project/showfiles.php?group_id=109543&package_id=11836 ...
- P2946 [USACO09MAR]牛飞盘队Cow Frisbee Team
题目描述 After Farmer Don took up Frisbee, Farmer John wanted to join in the fun. He wants to form a Fri ...
- API介绍
API(Application Programming Interface,应用程序编程接口)是一些预先定义的函数,目的是提供应用程序与开发人员基于某软件或硬件得以访问一组例程的能力,而又无需访问源码 ...
- k8s-StatefulSet控制器-十四
一.StatefulSet概述 RC.Deployment.DaemonSet都是面向无状态的服务,它们所管理的Pod的IP.名字,启停顺序等都是随机的,而StatefulSet管理所有有状态的服务, ...
- 任务47:Identity MVC:ReturnUrl实现
任务47:Identity MVC:ReturnUrl实现 在最上面加一个私有的方法 登陆也加上returnUrl Login的post方法.加入returnUrl的参数 登陆界面也需要加上 asp- ...
- Tenka1 Programmer Beginner Contest D - IntegerotS(位运算)
传送门 题意 给出N,K,给出N对数a[i],b[i],选择一些数使得or和小于k且\(max\sum b[i]\) 分析 枚举k的每一个1位,将其删去并让低位全为1,对于每一个这样的数c,如果a[i ...
- IT兄弟连 JavaWeb教程 Servlet转发
Servlet对象由Servlet容器创建,并且Servlet对象的service()方法也由容器调用,一个Servlet对象可否直接调用另一个Servlet对象的service()方法呢?答案是否定 ...