C - Tour UVA - 1347

C - Tour

  - 1347 

John Doe, a skilled pilot, enjoys traveling. While on vacation, he rents a small plane and starts visitingbeautiful places. To save money, John must determine the shortest closed tour that connects hisdestinations. Each destination is represented by a point in the plane pi =< xi, yi >. John uses thefollowing strategy: he starts from the leftmost point, then he goes strictly left to right to the rightmostpoint, and then he goes strictly right back to the starting point. It is known that the points havedistinct x-coordinates.Write a program that, given a set of n points in the plane, computes the shortest closed tour thatconnects the points according to John’s strategy

C - Tour UVA - 1347_第1张图片


解题思路:

1.“从左到右再回来不方便思考”,我们把它改成两个人同时从最左端出发,然后走到最右端,起点和终点相同。要求这两个人走的路程的总和的最小值。

2.我们用d(i,j)表示第一个人和第二个人走到i和j时(如果i>j,i前面的点都已经走过,如果i

3.d(i,j)表示1—max(i,j)的点都已经走过,那么这两个人的下一步一定是往下走到i+1,那么可能是i~i+1,也可能是j~i+1,我们知道d(i,j)=d(j,i),那么我们统一规定把i,j中大的放在前面。那么当由i~i+1的时候,dp(i,j)~dp(i+1,j);由j~i+1的时候,dp(i,j)~dp(i+1,i);状态转移的时候要加上它们转移需要的路程dis(i,i+1)或者dis(j,i+1)哪个小就取哪个。

那么我们就知道了状态转移方程了是不是 dp[i][j]=min(dp[i+1][j]+dis(i,i+1),dp[i+1][i]+dis(j,i+1));

4.在整个过程中我们要清楚的是dp[i][j],i>j,我们在扫描的时候i用的是逆序,因为dp[n][n]=0;然后我们走的远的i为基准,j在任意一个位置都是不重要的,对j从1开始扫描。

代码如下: 这个真正是我的思路的代码。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define inf 0x3f3f3f;
using namespace std;

struct Node
{
    int x,y;
}node[1000];
bool cmp(Node a,Node b)
{
    return a.x=1;i--)   //i 从n-2开始
     {
         for(int j=1;j

你可能感兴趣的:(动态规划)