uva1347-Tour

出處https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4093

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 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 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 xand 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

3 1 1 2 3 3 1 4 1 1 2 3 3 1 4 2

Sample Output

6.47 7.89

解題策略

dis[i][j]表示第一條走到最後一點第i點,而走到最後一點第j點,目前已經從第0點走到(第i點與第j點數值較大的點),每個點都走過一次的最短距離,k=max(i,j)+1表示k為下一個點,點k可以接到第一條dis[k][j]也可以接到第二條dis[i][k],最後計算dis[i][n-1]與dis[n-1][i],加上點i到點n-1的距離,i=0到(n-1)

參考資料:http://mypaper.pchome.com.tw/zerojudge/post/1324622750

待完成或參考程式碼

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

/*dis[i][j]表示第一條走到最後一點第i點,而走到最後一點第j點,目前已經從第0點走到(第i點與第j點數值較大的點),每個點都走過一次的最短距離,k=max(i,j)+1表示k為下一個點,點k可以接到第一條dis[k][j]也可以接到第二條dis[i][k],最後計算dis[i][n-1]與dis[n-1][i],加上點i到點n-1的距離,i=0到(n-1) 參考資料:http://mypaper.pchome.com.tw/zerojudge/post/1324622750 */#include <cstdio>#include <cmath>using namespace std; double dis[200][200]; int x[200],y[200]; int max(int i,int j){ if (i>j) return i; else return j; } double dist(int d1,int d2){ return sqrt(d1*d1+d2*d2); } int main(){ int n,k; double ans; while(scanf("%d",&n)==1){ for(int i=0;i<n;i++){ scanf("%d",x+i); scanf("%d",y+i); } for(int i=0;i<200;i++){//先設為很大的數字 for(int j=0;j<200;j++){ dis[i][j]=9999999999.0; } } ans=9999999999.0; dis[0][0]=0; for(int i=0;i<(n-1);i++){ for(int j=0;j<(n-1);j++){ k=max(i,j)+1;//k為下一個點 if (dis[k][j] > dis[i][j]+dist(x[k]-x[i],y[k]-y[i])) dis[k][j]=dis[i][j]+dist(x[k]-x[i],y[k]-y[i]);//k接在第一條 if (dis[i][k] > dis[i][j]+dist(x[k]-x[j],y[k]-y[j])) dis[i][k]=dis[i][j]+dist(x[k]-x[j],y[k]-y[j]);//k接在第二條 } } for(int i=0;i<n;i++){ if (ans > dis[i][n-1]+dist(x[n-1]-x[i],y[n-1]-y[i])) ans=dis[i][n-1]+dist(x[n-1]-x[i],y[n-1]-y[i]);//第一條走到i,第二條走道n-1,所有點都走過,加上第n-1點走道第i點形成一個迴路,找出路徑和最小 if (ans > dis[n-1][i]+dist(x[n-1]-x[i],y[n-1]-y[i])) ans=dis[n-1][i]+dist(x[n-1]-x[i],y[n-1]-y[i]);//第一條走到n-1,第二條走道i,所有點都走過,加上第n-1點走道第i點形成一個迴路,找出路徑和最小 } printf("%.2lf\n",ans); } }