HDU 2224 The shortest path_TSP旅行商问题_DP
2014-02-20 10:30
405 查看
原题:
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 654 Accepted Submission(s): 335
Problem Description
There are n points on the plane, Pi(xi, yi)(1 <= i <= n), and xi < xj (i<j). You begin at P1 and visit all points then back to P1. But there is a constraint:
Before you reach the rightmost point Pn, you can only visit the points those have the bigger x-coordinate value. For example, you are at Pi now, then you can only visit Pj(j > i). When you reach Pn, the rule is changed, from now on you can only visit the points
those have the smaller x-coordinate value than the point you are in now, for example, you are at Pi now, then you can only visit Pj(j < i). And in the end you back to P1 and the tour is over.
You should visit all points in this tour and you can visit every point only once.
Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n(2 <= n <= 200), means the number of points. Then following n lines each containing two positive integers Pi(xi, yi), indicating the coordinate of the i-th
point in the plane.
Output
For each test case, output one line containing the shortest path to visit all the points with the rule mentioned above.The answer should accurate up to 2 decimal places.
Sample Input
Sample Output
题意+题解:
1到N个点,给定坐标, 从左到右每次只能遍历较大的点,从右到左每次只能遍历更小的点,除了1号结点外所有结点只能且必须遍历一次,从1到N,再回到1
当时反正是做不出来,知道是TSP问题,看了ppt,看了题解才照着模版写了一遍。
基本思想是做两次tsp,都从左向右做。
dp[i][j]表示从1到i,从1到j两个TSP最短路程和,保证 1到i 和 1到j (除了 1 和 当i==j 时的i)都只被遍历一遍,则dp
即为答案,同时要求(j>=i)
那么状态转移一共有3中情况:
i==j dp[i][i] = dp[i-1][i] + dis(i-1,i);
i==j-1 dp[i][j]= min(dp[i][j] , dp[k][i]+dis(k,j)); 1--i + 1---i+1 == 1---k + 1---i +k---j 【k到j中其他点都被遍历过了,所以直接就是k和j的距离】(1<=k<=i-1)
i<j-1 dp[i][j] = dp[i][j-1] + dis(j-1.j);
代码:
The shortest path
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 654 Accepted Submission(s): 335
Problem Description
There are n points on the plane, Pi(xi, yi)(1 <= i <= n), and xi < xj (i<j). You begin at P1 and visit all points then back to P1. But there is a constraint:
Before you reach the rightmost point Pn, you can only visit the points those have the bigger x-coordinate value. For example, you are at Pi now, then you can only visit Pj(j > i). When you reach Pn, the rule is changed, from now on you can only visit the points
those have the smaller x-coordinate value than the point you are in now, for example, you are at Pi now, then you can only visit Pj(j < i). And in the end you back to P1 and the tour is over.
You should visit all points in this tour and you can visit every point only once.
Input
The input consists of multiple test cases. Each case begins with a line containing a positive integer n(2 <= n <= 200), means the number of points. Then following n lines each containing two positive integers Pi(xi, yi), indicating the coordinate of the i-th
point in the plane.
Output
For each test case, output one line containing the shortest path to visit all the points with the rule mentioned above.The answer should accurate up to 2 decimal places.
Sample Input
3 1 1 2 3 3 1
Sample Output
6.47 Hint: The way 1 - 3 - 2 - 1 makes the shortest path.
题意+题解:
1到N个点,给定坐标, 从左到右每次只能遍历较大的点,从右到左每次只能遍历更小的点,除了1号结点外所有结点只能且必须遍历一次,从1到N,再回到1
当时反正是做不出来,知道是TSP问题,看了ppt,看了题解才照着模版写了一遍。
基本思想是做两次tsp,都从左向右做。
dp[i][j]表示从1到i,从1到j两个TSP最短路程和,保证 1到i 和 1到j (除了 1 和 当i==j 时的i)都只被遍历一遍,则dp
即为答案,同时要求(j>=i)
那么状态转移一共有3中情况:
i==j dp[i][i] = dp[i-1][i] + dis(i-1,i);
i==j-1 dp[i][j]= min(dp[i][j] , dp[k][i]+dis(k,j)); 1--i + 1---i+1 == 1---k + 1---i +k---j 【k到j中其他点都被遍历过了,所以直接就是k和j的距离】(1<=k<=i-1)
i<j-1 dp[i][j] = dp[i][j-1] + dis(j-1.j);
代码:
//大二寒假集训 第一章DP F_The shortest path_双调旅行商TSP #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<iostream> #include<algorithm> using namespace std; double dp[205][205];//dp[i][j](j>=i)表示从i走到1,再折返到j,所用最小路程和,且满足题目要求每个点走且只走一次 typedef struct MyStruct { int x,y; }POINT; int n; POINT point[205]; double dis(POINT a,POINT b) { return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y))); } double find_min(double a,double b) { return a<b?a:b; } int main() { int i,j,k; while (scanf("%d",&n)!=EOF) { for ( i = 0; i <=n ; i++) for ( j = 0; j <=n ; j++) dp[i][j]=9999999.0; for(i=1;i<=n;i++) scanf("%d%d",&point[i].x,&point[i].y); dp[1][2]=dis(point[1],point[2]); for ( i = 1; i <=n ; i++) { for ( j = i; j <=n ; j++) { if(i==1 && (j==2 || j==1))continue; if(i==j)dp[i][j]=dp[i-1][j]+dis(point[i-1],point[j]); else if(i==j-1) { for ( k = 1; k <j-1 ; k++) { dp[i][j]=find_min(dp[i][j],dp[k][j-1]+dis(point[k],point[j])); } } else { dp[i][j]=dp[i][j-1]+dis(point[j-1],point[j]); } } } printf("%.2lf\n",dp ); } return 0; }
相关文章推荐
- HDU - 2224 The shortest path (双调欧几里德旅行商问题)
- HDU 2224 The shortest path(双调欧几里得旅行商问题)
- HDU 2224 The shortest path 双调旅行商问题
- hdu 2224 The shortest path 单向旅行商问题
- hdu_2224_The shortest path(dp)
- HDU 2224 The shortest path (TSP模板)
- hdu_2224_The shortest path(dp)
- hdu-2224-The shortest path-双调TSP
- Hdu 2224 The shortest path(双调TSP)
- hduThe shortest path(双调欧几里得旅行商问题)
- hdu 2224 双调欧几里得旅行商问题tsp
- HDOJ 题目2224 The shortest path||POJ 题目2677 Tour(双调TSP)
- HDU 2224 The shortest path
- hdu 2224 经典DP 双调旅行商问题
- hdu 2224 The shortest path
- The shortest path-dp+tsp
- The shortest path---hdu2224 && Tour---poj2677(旅行商问题)
- hdu 224 The shortest path(线性DP)
- hdu 2224 The shortest path
- hdoj 2224 && poj 2677 The shortest path 【TSP】