problem Uva1347-touraccept: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, 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 le. Each data set in the Le 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.
Outputfor each set of data, your program should print the result to the standard output from the beginning of a line. The tour length, a? Oating-point number with the fractional digits, represents the result.
Note:an Input/output sample is in the table below. Here there is the data sets. The. RST one contains 3 points speci?ed 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 RST data set in the given example). Sample INPUT3
1 1
2 3
3 1
4
1 1
2 3
3 1
4 2 Sample Output
6.47
7.89
The problem: Although the code is relatively short, but the definition of the state is really very show, do not know the next time you encounter a similar question can you think of the definition of state. First of all, from left to right and from right to left generally are two from left to right, Dp[i][j] for the first person to I, the second person to J, and the label <=min (I,J) have gone through, it is obvious dp[i][j] = = Dp[j][i], it may be prescribed i > J, so the DP I [j] can only be transferred from Dp[i+1][i], or dp[i+1][j], memory search is good.
1#include <bits/stdc++.h>2 3 using namespacestd;4 5 Const intMAXN = ++Ten;6 7 intN;8 9 structPoint {Ten intx, y; One }POINT[MAXN]; A - DoubleDIST[MAXN][MAXN]; - DoubleDP[MAXN][MAXN]; the - DoubleDP (intIintj) { - Double& ans =Dp[i][j]; - if(Ans >0)returnans; + -Ans =0.0; + if(i = = N-1) { AAns = dist[n-1][n] +Dist[j][n]; at } - Else { -ans = min (dist[i][i +1] + DP (i +1, j), Dist[j][i +1] + DP (i +1, i)); - } - returnans; - } in - intMain () to { + //freopen ("Input.txt", "R", stdin); - while(~SCANF ("%d", &n) &&N) { the for(inti =1; I <= N; i++) { *scanf"%d%d", &point[i].x, &point[i].y); $ }Panax Notoginseng - for(inti =1; I <= N; i++) { the for(intj =1; J <= N; J + +) { +DP[I][J] =-1.0; ADist[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)); the } + } - $printf"%.2f\n", DP (1,1)); $ } - return 0; -}
Uva1347-tour (Dynamic planning Basics)