UVA 1347. Tour 动态规划

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 that connects his destinations.

Each destination is represented by a point in the plane p i = ( x i , y i ) p_{i} = (x_{i}, y_{i}) 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 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 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
\newline
\newline
\newline

思路1

这题自己做得有点懵懂,很多细节都感觉没考虑清楚,有点云里雾里的感觉。但是提交上去居然AC了,没想到啊。。。

先说说我自己做这个题的时候的想法,因为每个点都需要被遍历,或者是在从左到右,或者从右到左。有点像0 - 1背包问题,只不过这里有两个背包,如果第一个背包不取当前的点,那么第二个背包必须取。所以一开始想构造第一个背包从左到右遍历到前 i 个点的时候,最小的距离是多少这样的状态。但是发现状态转移方程很难写,因为不知道前面一个点是啥。。。OK,那我改变一下,定义状态:第一个背包从左往右遍历到第 i 个点且第 i 个点必须取的时候,最小的距离。这样就解决了刚刚 “不知道前一个点是啥” 这个问题。但是现在还是很难写状态转移方程,因为不知道第二个背包里面有啥东西,虽然我知道第二个背包和第一个背包互补,但是具体怎么反映到程序上是个问题。

这儿纠结了一会儿,我想到干脆用一个二维变量 ( i , j ) (i, j) (i,j) 表示第一个背包最后一个取的点为 i,第二个背包最后一个取的点为 j
这样如果有 i > j i > j i>j,那么再分两种情况,如果 j < i − 1 j < i - 1 j<i1,那么 i − 1 i - 1 i1 这个点必然是属于第一个背包,那么必然有:
d p [ i ] [ j ] = d p [ i − 1 ] [ j ] + d i s t ( i − 1 , i ) dp[i][j] = dp[i - 1][j] + dist(i - 1, i) dp[i][j]=dp[i1][j]+dist(i1,i)
如果 j = = i − 1 j == i - 1 j==i1,那么 ( i , j ) (i, j) (i,j) 可以由 ( k , j ) ,   0 ≤ k < j (k, j), \space 0 \le k < j (k,j), 0k<j,转移而来。
d p [ i ] [ j ] = m i n   0 ≤ k < j   d p [ k ] [ j ] + d i s t ( k , i ) dp[i][j] = min_{\space 0 \le k < j} \space dp[k][j] + dist(k, i) dp[i][j]=min 0k<j dp[k][j]+dist(k,i)
对于 i < j i < j i<j 的情况也是同理。这样一个就是 O ( n 3 ) O(n^{3}) O(n3) 时间。

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    private double[] x;
    private double[] y;
    private int n;
    private double[][] f;

    private double solve(double[] x, double[] y) {
        this.n = x.length;
        this.x = x;
        this.y = y;
        f = new double[n][n];
        double ans = Double.MAX_VALUE;
        for (double[] ff : f) {
            Arrays.fill(ff, Double.MAX_VALUE);
        }
        for (int j = 0; j < n - 2; ++j) {
            ans = Math.min(ans, helper(n - 1, j) + dist(j, n - 1));
        }
        for (int i = 0; i < n - 2; ++i) {
            ans = Math.min(ans, helper(i, n - 1) + dist(i, n - 1));
        }
        return ans;
    }

    private double helper(int i, int j) {
        if (f[i][j] != Double.MAX_VALUE) {
            return f[i][j];
        }
        if (i == 0 && j == 0) {
            return f[i][j] = 0.0;
        }
        if (i == 0) {
            return f[i][j] = helper(i, j - 1) + dist(j - 1, j);
        }
        if (j == 0) {
            return f[i][j] = helper(i - 1, j) + dist(i - 1, i);
        }
        if (i < j) {
            if (i < j - 1) {
                f[i][j] = helper(i, j - 1) + dist(j - 1, j);
            } else {
                for (int k = 0; k < i; ++k) {
                    f[i][j] = Math.min(f[i][j], helper(i, k) + dist(k, j));
                }
            }
        } else {
            if (j < i - 1) {
                f[i][j] = helper(i - 1, j) + dist(i - 1, i);
            } else {
                for (int k = 0; k < j; ++k) {
                    f[i][j] = Math.min(f[i][j], helper(k, j) + dist(k, i));
                }
            }
        }
        return f[i][j];
    }

    private double dist(int i, int j) {
        return Math.sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
    }

    public static void main(String[] args) {
        Main main = new Main();
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            double[] x = new double[n];
            double[] y = new double[n];
            for (int i = 0; i < n; ++i) {
                x[i] = sc.nextDouble();
                y[i] = sc.nextDouble();
            }
            System.out.printf("%.2f\n", main.solve(x, y));
        }
    }
}

\newline
\newline
\newline

思路2

上面的方法比较蹩脚,下面说一下我从网上看到的大多数人采用的比较优雅的方法。

首先把问题转化为,有两个人各自从起点出发,去往终点,然后定义状态 d p [ i ] [ j ] dp[i][j] dp[i][j] 为第一个人在点 i, 第二个人在点 j 且起点到 m a x ( i , j ) max(i, j) max(i,j) 之间所有的点都已经遍历

这一步状态定义非常 aggressive,如果从我上面方法的背包角度去考虑,确实挺好理解,但是突然跳到这一步其实挺难,可能需要大量的经验积累,才能一下子想到这一步。

  1. 把问题由从左到右和从右到左,转化为两个从左到右。这个如果用背包模型考虑,很好理解。毕竟路线的顺序不重要,重要的只是:到底每个点应该是放到第一个背包还是第二个背包,换句话说,也就是每个点到底应该让第一个人遍历,还是第二个人遍历。遍历的顺序是从左往右或者从右往左不重要。
  2. 定义起点到 m a x ( i , j ) max(i, j) max(i,j) 之间所有的点都已经遍历。这个用背包模型也很好理解,其实就是从第一个点开始往后,分配每一个点到第一个还是第二个背包,分配完了再往后。所以起点到 m a x ( i , j ) max(i, j) max(i,j) 之间的点都已经遍历是一个很合理的规定。

然后状态转移方程:
d p [ i ] [ j ] = m i n ( d p [ i + 1 ] [ j ] + d i s t ( i , i + 1 ) ,   d p [ i ] [ i + 1 ] + d i s t ( j , i + 1 ) ) dp[i][j] = min(dp[i + 1][j] + dist(i, i + 1), \space dp[i][i + 1] + dist(j, i + 1)) dp[i][j]=min(dp[i+1][j]+dist(i,i+1), dp[i][i+1]+dist(j,i+1))
这里,我们要假设 i ≥ j i \ge j ij,然后因为第 i + 1 i +1 i+1 个点一定要分配给第一个人(背包)或者第二个人(背包),所以有上面的状态转移方程。观察到第一个人和第二个人路线具有对称性,即 d p [ i ] [ j ] = d p [ j ] [ i ] dp[i][j] = dp[j][i] dp[i][j]=dp[j][i], 所以有:
d p [ i ] [ j ] = m i n ( d p [ i + 1 ] [ j ] + d i s t ( i , i + 1 ) ,   d p [ i + 1 ] [ i ] + d i s t ( j , i + 1 ) ) dp[i][j] = min(dp[i + 1][j] + dist(i, i + 1), \space dp[i + 1][i] + dist(j, i + 1)) dp[i][j]=min(dp[i+1][j]+dist(i,i+1), dp[i+1][i]+dist(j,i+1))

算法时间复杂度为 O ( n 2 ) O(n^{2}) O(n2)

import java.util.Scanner;

public class Main {
    private double[] x;
    private double[] y;

    private double solve(double[] x, double[] y) {
        int n = x.length;
        this.x = x;
        this.y = y;
        double[] dp = new double[n];
        for (int i = 0; i < n; ++i) {
            dp[i] = dist(i, n - 1);
        }
        for (int i = n - 2; i >= 0; --i) {
            for (int j = 0; j <= i; ++j) {
                dp[j] = Math.min(dp[j] + dist(i, i + 1), dp[i] + dist(j, i + 1));
            }
        }
        return dp[0];
    }

    private double dist(int i, int j) {
        return Math.sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
    }

    public static void main(String[] args) {
        Main main = new Main();
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = sc.nextInt();
            double[] x = new double[n];
            double[] y = new double[n];
            for (int i = 0; i < n; ++i) {
                x[i] = sc.nextDouble();
                y[i] = sc.nextDouble();
            }
            System.out.printf("%.2f\n", main.solve(x, y));
        }
    }
}

你可能感兴趣的:(Dynamic,Programming)