Tour
Time Limit: the MS 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 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 P Oints according 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 coordinate. 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.
Note: An Input/output sample are in the table below. Here there is the data sets. The first one contains 3 points specified by theirx and y coordinates. The second point, for example, has thex 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
3 1 12 33 14 1 1 2 33 14 2
Sample Output
6.477.89
Test instructions: Typical examples of dynamic programming. It is also called the Double-tune Euclid travel quotient problem. The topic in the introduction of algorithms.
Ideas:
DP[I][J] represents the distance from I to 1, and then from 1 to J. on this path, all points between points 1 to Pmax (I,J) have and only once.
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));
<span style= "FONT-SIZE:18PX;" > #include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include < string> #include <algorithm> #include <queue> #include <stack>using namespace Std;const int INF = 1 <<29;const int maxn = 1100;const Double PI = ACOs ( -1.0); const double e = 2.718281828459;const double EPS = 1e-8;stru CT node{double x; Double y;} A[MAXN];d ouble dp[maxn][maxn];int CMP (Node A, Node B) {return a.x < b.x;} Double dist (int i, int j) {return sqrt ((a[i].x-a[j].x) * (a[i].x-a[j].x) + (A[I].Y-A[J].Y) * (A[I].Y-A[J].Y));} int main () {//freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); int n; while (Cin>>n) {for (int i = 1; I <= n; i++) {scanf ("%lf%lf", &a[i].x, &a[i] . y); } sort (a+1, a+1+n, CMP); DP[2][1] = dist (1, 2); for (int i = 3; I <= n; i++) {dp[i][i-1] = inf*1.0; for (Int J= 1; J < I-1; J + +) {dp[i][i-1] = min (dp[i][i-1], dp[i-1][j]+dist (i, j)); DP[I][J] = dp[i-1][j]+dist (i-1, i); }} double ans = inf*1.0; for (int i = 1; i < n; i++) {ans = min (ans, dp[n][i]+dist (n, i)); } printf ("%.2f\n", ans); } return 0;} </span>
UVA 1347 (POJ 2677) Tour (double-tune Euclid Travel quotient issue)