Problem UVA1347-Tour

Accept: 667  Submit: 3866
Time Limit: 3000 mSec

Problem Description

John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visiting beautiful places. To save money, John must determine the shortest closed tour that connects his destinations. Each destination is represented by a point in the plane pi =< xi,yi >. John uses the following strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmost point, and then he goes strictly right back to the starting point. It is known that the points have distinct x-coordinates. Write a program that, given a set of n points in the plane, computes the shortest closed tour that connects the points according to John’s strategy.

Input

The program input is from a text file. Each data set in the file stands for a particular set of points. For each set of points the data set contains the number of points, and the point coordinates in ascending order of the x coordinate. White spaces can occur freely in input. The input data are correct.

 Output

For each set of data, your program should print the result to the standard output from the beginning of a line. The tour length, a floating-point number with two fractional digits, represents the result.
Note: An input/output sample is in the table below. Here there are two data sets. The first one contains 3 points specified by their x and y coordinates. The second point, for example, has the x coordinate 2, and the y coordinate 3. The result for each data set is the tour length, (6.47 for the first data set in the given example).
 

 Sample Input

3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2
 

Sample Output

6.47

7.89

题解:这个题代码虽然比较短,但是状态的定义真的是很秀,不知道下一次再遇到类似的题自己能不能想到这样定义状态。首先,从左到右再从右到左一般都是化为两个从左到右,dp[i][j]为第一个人到i,第二个人到j,并且标号<=min(i,j)都已经走过,很显然dp[i][j] == dp[j][i],因此不妨规定i > j,因此dp[i][j]只能从dp[i+1][i],或dp[i+1][j]转移过来,记忆化搜索就好。

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 int n;

 struct Point {
int x, y;
}point[maxn]; double dist[maxn][maxn];
double dp[maxn][maxn]; double DP(int i, int j) {
double& ans = dp[i][j];
if (ans > ) return ans; ans = 0.0;
if (i == n - ) {
ans = dist[n - ][n] + dist[j][n];
}
else {
ans = min(dist[i][i + ] + DP(i + , j), dist[j][i + ] + DP(i + , i));
}
return ans;
} int main()
{
//freopen("input.txt", "r", stdin);
while (~scanf("%d", &n) && n) {
for (int i = ; i <= n; i++) {
scanf("%d%d", &point[i].x, &point[i].y);
} for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
dp[i][j] = -1.0;
dist[j][i] = dist[i][j] = sqrt(1.0*(point[i].x - point[j].x)*(point[i].x - point[j].x) + 1.0*(point[i].y - point[j].y)*(point[i].y - point[j].y));
}
} printf("%.2f\n", DP(, ));
}
return ;
}

UVA1347-Tour(动态规划基础)的更多相关文章

  1. UVA-1347 Tour 动态规划 难以确定的状态

    题目链接:https://cn.vjudge.net/problem/UVA-1347 题意 给出按x坐标排序的几个点. 欲从最左边不回头的走到最右边,然后再返回最左边. 每个点都要被访问,且只能经过 ...

  2. nyist oj 79 拦截导弹 (动态规划基础题)

    拦截导弹 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描写叙述 某国为了防御敌国的导弹突击.发展中一种导弹拦截系统.可是这样的导弹拦截系统有一个缺陷:尽管它的第一发炮弹可以 ...

  3. Problem C: 动态规划基础题目之数字三角形

    Problem C: 动态规划基础题目之数字三角形 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 208  Solved: 139[Submit][Sta ...

  4. F - Free DIY Tour(动态规划,搜索也行)

    这道题可用动态规划也可以用搜索,下面都写一下 Description Weiwei is a software engineer of ShiningSoft. He has just excelle ...

  5. Chapter_9 DP : uva1347 tour (bitonic tour)

    https://cn.vjudge.net/problem/UVA-1347 这道题居然可以O(n^2)解决, 让我太吃惊了!!! 鄙人见识浅薄, 这其实是一个经典问题: bitonic tour. ...

  6. Codeforces Flipping game 动态规划基础

    题目链接:http://codeforces.com/problemset/problem/327/A 这道题目有O(N^3)的做法,这里转化为动态规划求解,复杂度是O(N) #include < ...

  7. UVA437-The Tower of Babylon(动态规划基础)

    Problem UVA437-The Tower of Babylon Accept: 3648  Submit: 12532Time Limit: 3000 mSec Problem Descrip ...

  8. 《挑战程序设计竞赛》2.3 动态规划-基础 POJ3176 2229 2385 3616 3280

    POJ3176 Cow Bowling 题意 输入一个n层的三角形,第i层有i个数,求从第1层到第n层的所有路线中,权值之和最大的路线. 规定:第i层的某个数只能连线走到第i+1层中与它位置相邻的两个 ...

  9. UVa1347 Tour

    /*----UVa1347 ---首相两边方向走不方便,可以看做:两个人同时从最左边出发,沿着两条不同路径走到终点,除了起点和中点外 其他点恰好被走过一遍 ---用dp[i][j]表示1-max(i, ...

随机推荐

  1. JavaAndroid开发部分API

    JavaAndroid开发中的部分系统API 四大组件,都需要在清单文件中配置 Activity: 用来提供一个能让用户操作并与之交互的界面 onCreate(): 自动调用的方法, 在其中加载布局显 ...

  2. Https协议报错:com.sun.net.ssl.internal.www.protocol.https.HttpsURLConnectionOldImpl解决方法

    旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/82220333 所用应用服务器:JBoss服务 ...

  3. 通过JS生成由字母与数字组合的随机字符串

    在项目中可能需要随机生成字母数字组成的字符,如生成3-32位长度的字母数字组合的随机字符串(位数不固定)或者生成43位随机字符串(位数固定) 使用Math.random()与toString()方法的 ...

  4. 洛谷P4561 [JXOI2018]排序问题(二分 期望)

    题意 题目链接 Sol 首先一种方案的期望等于它一次排好的概率的倒数. 一次排好的概率是个数数题,他等于一次排好的方案除以总方案,也就是\(\frac{\prod cnt_{a_i}!}{(n+m)! ...

  5. 2018-08-13 Head First OO分析设计一书略读与例子中文化

    注: 此笔记仅为个人学习此教程的布局和材料组织之用. 如有兴趣请自行详阅. 第一章是以吉他商店的存货系统作例子. 第二章设计有狗洞的门. 第三章对第二章基础上, 更改需求后对应设计. 第四章继续改进此 ...

  6. 小程序实践(五):for循环绑定item的点击事件

    微信展示列表效果借助于 wx:for  简单写一个列表(wxml文件中): 对应的数据源(js文件中): 写一个点击监听: 效果: 以上.可以实现列表的item点击效果,但是无法到点击的item对应的 ...

  7. Spinner的简单使用

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  8. (其他)用sublime text3编写的html网页用浏览器打开出现中文乱码的原理及解决方法(转)

    最近发现Hbuler比较难用,换成sublime text3了,用了以前没用过的软件,就要学习他的操作,刚上手就出了点问题. 解决方法就是sublime text3以utf8 with bom保存. ...

  9. Git:二、本地文件操作

    文件必须放在本地Git仓库的文件夹下,子文件夹也可以. 1.添加/修改 git add <文件名> 2.提交 git commit -m "本次提交说明" 可以add很 ...

  10. spring4笔记----spring生命周期属性

    init-method : 指定bean的初始化方法-spring容器会在bean的依赖关系注入完成后调用该方法 destroy-method :指定bean销毁之前的方法-spring容器将会在销毁 ...