UVa 1347 Tour

Tour

Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

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 = < xiyi ><tex2html_verbatim_mark> . 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<tex2html_verbatim_mark> -coordinates.

Write a program that, given a set of n<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> 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<tex2html_verbatim_mark> and y<tex2html_verbatim_mark>coordinates. The second point, for example, has the x<tex2html_verbatim_mark> coordinate 2, and the y<tex2html_verbatim_mark> 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

可以把一个人的往返看作是两个人从起点同时出发走到终点,两人走到的点不重复

则对于一个点,要么一个人过去,要么另一个人过去(不需要区分到底是哪个人)

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 using namespace std;
 8 const int mxn=1024;
 9 int x[mxn],y[mxn];
10 double mp[mxn][mxn];
11 double f[mxn][mxn];
12 int n;
13 int main(){
14     while(scanf("%d",&n)!=EOF){
15         memset(f,11,sizeof(f));
16         int i,j;
17         for(i=1;i<=n;i++){
18             scanf("%d%d",&x[i],&y[i]);
19         }
20         for(i=1;i<=n;i++){
21             for(j=1;j<=n;j++){
22                 mp[i][j]=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
23             }
24         }
25         for(i=n-1;i>=2;i--){//i倒推
26             for(j=1;j<i;j++){
27                 if(i==n-1)f[i][j]=mp[i][n]+mp[j][n];//边界
28                 else f[i][j]=min(mp[i][i+1]+f[i+1][j],mp[j][i+1]+f[i+1][i]);
29             }
30         }
31         printf("%.2f\n",mp[1][2]+f[2][1]);
32         //最终状态是一个人走到了点2,一个人走到了点1,需要再加上从2到1的距离
33     }
34     return 0;
35 }
时间: 2024-10-18 22:58:33

UVa 1347 Tour的相关文章

UVA 1347 Tour DP

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=446&page=show_problem&problem=4093 题目描述: 有N个点, 要求求一个回路的最短距离.(N <= 1000) 解题思路: 一开始想暴力的, 因为实在想不出其他方法了啊....走一圈周长最短可以等价为两个人同时从最左端出发,沿着不同的路径走到最右端.如果定义d(i,j)

UVA - 1347 Tour 双调欧几里得旅行商问题

题目大意:给出n个点,要求你从最左边那个点走到最右边那个点,每个点都要被遍历过,且每个点只能走一次,问形成的最短距离是多少 解题思路:用dp[i][j]表示第一个人走到了第i个点,第二个人走到了第j个点且已经遍历了1–max(i,j)的所有点的最短距离.因为dp[i][j] = dp[j][i]的,所以我们设i > j的 那么就有 当j < i-1 时,dp[i][j] = dp[i-1][j] + dis(i, i -1) 当j == i + 1时情况就比较特别了,这里将j用i-1代替 dp

uva 1347 - Tour(双调欧几里得)

题目大意:给出n个点,确定一条 连接各点的最短闭合旅程的问题. 解题思路:dp[i][j]表示说从i联通到1,再从1联通到j的距离. dp[i][j] = dp[i-1][j] + dis(i,i-1); dp[i][i-1] = min (dp[i][i-1], dp[i-1][j] + dis(i, j)); 记忆化代码: //0 KB 58 ms #include<cstdio> #include<iostream> #include<cstring> #incl

Tour UVA - 1347

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 b

uva 1347 poj 2267 Tour 最短双调回路

// uva1347 Tour 最短双调路线 // 这道题是看着紫书上面写着的 // dp[i][j]表示1至max(i,j)都已经走过时并且第一个人在i // 第二个人在j点时所要走的最短的距离,则dp[i][j] = dp[j][i] // 状态转移方程为 // dp[i+1][j] = max(dp[i][j]+dist[i][i+1],dp[i+1][i]+dist[j][i+1]) // 其实就是考虑第i+1号点是与i相连还是与j相连,(本来dp[i+1][i]是dp[i][i+1]

UVA 1347(POJ 2677)Tour(双调欧几里得旅行商问题)

Tour                 Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu 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 mus

UVA 1347(POJ 2677) Tour(双色欧几里德旅行商问题)

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 r

UVa 1347 (双线程DP) Tour

题意: 平面上有n个坐标均为正数的点,按照x坐标从小到大一次给出.求一条最短路线,从最左边的点出发到最右边的点,再回到最左边的点.除了第一个和最右一个点其他点恰好只经过一次. 分析: 可以等效为两个人从第一个点出发,沿不同的路径走到最右点. d(I, j)表示点1~max(I, j)这些点全部都走过,而且两人的位置分别是i和j,最少还需要走多长的距离.由这个定义可知,d(I, j) == d(j, i),所以我们再加一个条件,d(I, j)中i>j 这样状态d(I, j)只能转移到d(i+1,

uva 1347

Tour Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status 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