[ACM] poj 2677 Tour (Dynamic Planning, dual-tuning Euclidean Traveling Salesman Problem)

Source: Internet
Author: User

Tour
Time limit:1000 ms   Memory limit:65536 K
Total submissions:3585   Accepted:1597

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 participant 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 shocould 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. 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

31 12 33 141 12 33 14 2

Sample output

6.477.89

Source

Southeastern Europe 2005


Solution:

Reproduced in: http://blog.sina.com.cn/s/blog_51cea4040100gkcq.html

The Euclidean Traveling Salesman Problem is the problem of determining the shortest closed journey of each point on the given n points on the plane. (A) A solution of seven points is given. The general form of this problem is NP-complete, so the solution requires more time than polynomial.

   J. l. bentley suggests simplifying the problem by only considering bitonic tour, which is a journey from the leftmost point to the rightmost point, strictly from left to right, then strictly from right to left to start point. (B) shows the shortest double-adjustment routes of the same seven points. In this case, polynomial algorithms are possible. In fact, there is an O (N * n) Time Algorithm for determining the optimal dual-tuning route.

 

   Note: seven points on a plane are displayed on a grid. A) The shortest closed route. The length is about 24.89. This route is not dual-tuned. B) The shortest two-way closure route on the set with the same points. It is about 25.58 in length.

 

   This is a question 15-1 in computing.

   First, sort the given vertex. The key word X is re-numbered, from left to right, 1, 2, 3 ,..., N.

   Define P [I] [J] to indicate the distance between node I and node J.

   Define d [I] [J] To connect from I to 1 and then from 1 to J. (Note that I> J is not connected .)

   For any point I, there are two connection methods, one is (a), I is connected to the I-1, the other is (B ), I is not connected to the I-1.

   Based on the double-tuning journey, we know that node n must be connected to N. Then, if we want d [N] [n-1], simply add P [n-1] [N] to it is the shortest double-adjustment closed route.

   According to, it is easy to write an equation:

   D [I] [J] = d [I-1] [J] + P [I] [I-1];

   D [I] [I-1] = min (d [I-1] [J] + P [J] [I]);


To sum up the steps for solving this problem:

1. Define all points

2. Find the distance between any two points. You can write a separate function and call it at any time. You can also pre-process it. This "distance" has different meanings based on different questions.

3. Sort these points in ascending order of X coordinates

4. algorithm using double-tuned Euclidean Traveling Salesman Problem

Code:

# Include <iostream> # include <cmath> # include <iomanip> # include <algorithm> # include <string. h> using namespace STD; const int INF = 0x7fffffff; const int maxn = 1000; int N; // n vertices double DP [maxn] [maxn]; struct P {Double X, Y;} Point [maxn]; bool CMP (p a, p B) {if (. x <B. x) return true; return false;} double DIS (p a, p B) {return SQRT (. x-b.x) * (. x-b.x) +. y-b.y) * (. y-b.y);} double dp (int n) // double tuning Euclidean Traveling Salesman Problem algorithm, DP [N] [N] For the {sort (point + 1, point + 1 + N, CMP); DP [1] [2] = DIS (point [1], point [2]); For (Int J = 3; j <= N; j ++) {for (INT I = 1; I <= J-2; I ++) DP [I] [J] = DP [I] [J-1] + DIS (point [J-1], point [J]); DP [J-1] [J] = inf; for (int K = 1; k <= J-2; k ++) {double temp = DP [k] [J-1] + DIS (point [K], point [J]); If (temp <DP [J-1] [J]) DP [J-1] [J] = temp ;}} DP [N] [N] = DP [n-1] [N] + DIS (point [n-1], point [N]); Return DP [N] [N];} int main () {While (CIN> N) {for (INT I = 1; I <= N; I ++) CIN> point [I]. x> point [I]. y; cout <setiosflags (IOs: fixed) <setprecision (2) <dp (n) <Endl ;}return 0 ;}


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.