POJ2677 Tour (dp+ of the two-tone Euclid travel Quotient)

Source: Internet
Author: User

Tour
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 3929 Accepted: 1761

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, which connects his destinations. Each destination are represented by a point in the plane pi = < Xi,yi;. John uses the following strategy:he starts from the leftmost point and then he goes strictly Point, and then he goes strictly right back to the starting point. It is known that the points has distinct x-coordinates.
Write A program this, given a set of n points in the plane, computes the shortest closed tour that connects the points ACC Ording to John ' s strategy.

Input

The program input was 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 C Oordinate. White spaces can occur freely in input. The input data is 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 the fractional digits, represents the result. An Input/output sample are in the table below. Here there is the 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 was the tour length, (6.47 for the first data set in the given example).

Sample Input

31 12 33 141 12 33 14 2

Sample Output

6.477.89

Reference Blog: http://www.cnblogs.com/-sunshine/archive/2012/07/23/2605251.html
Thinking "Turn": Euclid's Travel quotient problem is the problem of determining the shortest closed journey of a connected point on a given n point on a plane. (a) a solution to the 7 point problem is given. The general form of the problem is NP-complete, so the solution needs more time than the polynomial.

J.L Bentley recommends simplifying the problem by considering only the dual-tune Journey (Bitonic Tour), which starts from the leftmost point, strictly from left to right up to the right, and then strictly from right to left until the starting point. (b) The shortest double alignment of the same 7 points is shown. In this case, the algorithm of the polynomial is possible. In fact, there is an algorithm for determining the optimal double-alignment of the O (n*n) time.

Figure A Figure B

Note: Seven points on a flat surface that are displayed on a single unit grid. A) The shortest closed route, which is approximately 24.89 in length. This route is not double-tuned. b) The shortest two-tone closed route on the set of the same point. The length is approximately 25.58.

This is a calculated study questions on the 15-1.

First will give the point sort, the keyword x, renumber, from left to right 1,2,3,...,n.

Define P[I][J], which represents the distance between node I and Node J.

Define D[I][J], which means to connect from I to 1, then from 1 to J, (note, i>j, and not connected.) )

For any point I, there are two methods of connection, one is shown in (a), I is connected to I-1, the other is (b), I and i-1 are not connected.

According to the two-tone journey, we know that the node n must be connected to n, so if we ask for the d[n][n-1], just add it p[n-1][n] is the shortest two-tone closed route.

According to this, it is easy to write equations:

DP[I][J]=DP[I-1][J]+DIST[I][I-1]; I this is i+1 walked to the

Dp[i][i-1]=min (Dp[i][i-1],dp[i-1][j]+dist[j][i]); I was walking from J to find the minimum value of J.

Their understanding: Rujia book on the idea, but the old card, take time to think about his ideas, this idea or understanding, in the dp[i][j] this position, to the next i+1, there are two situations either I go to, or J go, if I go to the corresponding first transfer equation, if J went to the DP[I][J] = = Dp[j][i], so corresponds to the second equation

#include <iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<cmath>using namespacestd;Const intMAX =10000+Ten;structpoints{Doublex, y;}; Points Point[max];DoubleD[max][max];intCMP (points A, points b) {return(B.x-a.x >0.00001);}DoubleMin (DoubleADoubleb) {    if(A-B >0.00001)        returnb; Else        returnA;}DoubleDistintAintb) {    returnsqrt ((point[a].x-point[b].x) * (point[a].x-point[b].x) + (POINT[A].Y-POINT[B].Y) * (POINT[A].Y-point[b].y));}intMain () {intN;  while(SCANF ("%d", &n)! =EOF) {         for(inti =1; I <= N; i++) scanf ("%LF%LF", &point[i].x, &point[i].y); Sort ( point+1, point + N +1, CMP); if(n = =1) {printf ("0\n"); Continue; } d[1][1] =0;  for(inti =1; I <= N; i++) d[i][1] = Dist (i,1);  for(inti =2; I < n; i++) {D[i+1][i] =10000000.0;  for(intj =1; J < I; J + +) {D[i+1][J] = D[i][j] + dist (i, i +1); D[i+1][i] = Min (D[i +1][i], D[i][j] + dist (j,i +1)); }} printf ("%.2lf\n", D[n][n-1] + dist (n-1, N)); }    return 0;}

POJ2677 Tour (dp+ of the two-tone Euclid travel Quotient)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.