[Programming questions] graduation trip problem

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/m1f2c3/article/details/99460372

[Programming questions] graduation trip questions
Time Limit: 1 seconds

Space limitations: 32768K

Xiao Ming is currently doing a graduation trip planning. Intended departure from Beijing, respectively, to a number of cities, and then returned to Beijing, between each city are riding high-speed rail, and to each city only once. Due to limited funds, hoping to arrange some of the expenses of the province on the road as much as possible by reasonable route. Given a set of train between the city and the price of each pair of cities, each city found only visited once and return to the starting point of a minimum fare expenses.

Input Description:
City number n (1 <n≤20, including Beijing)

Matrix m [n] ticket price n rows and n columns between cities [n]

Output Description:
The minimum fare s spending

Examples 1 Input:
. 4
0 2. 6. 5
2 0. 4. 4
. 6. 4 0 2
. 5. 4 0 2

Output Example 1:
13 is

Examples Description 1:
4 cities, fares cities and urban 1 1 0, 1 fare between the cities and urban 2, fare between 1 and 3 urban cities 6, cities and urban 1 fares between 4 and 5, and so on. Assuming that both can be purchased one-way ticket between any two cities, and the fare 1,000 yuan or less, regardless of extremes.

Time out, but this method does not know the difference compared with the matrix where dp

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] citys = new int[n][n];
        for(int i = 0; i < n; ++i){
            for(int j = 0; j < n; ++j){
                citys[i][j] = sc.nextInt();
            }
        }

        List<Integer> gone = new LinkedList<>();
        for(int i = 1; i < n; ++i) gone.add(i);
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < gone.size(); ++i){
            int temp_int = gone.get(i);
            gone.remove(i);
            min = Math.min(min, fun(temp_int, gone, citys) + citys[0][temp_int]);
            gone.add(i, temp_int);
        }

        System.out.println(min);
    }

    public static int fun(int head, List<Integer> gone, int[][] citys){
        if(gone.size() == 0){
            return citys[head][0];
        }
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < gone.size(); ++i){
            int temp_int = gone.get(i);
            gone.remove(i);
            min = Math.min(min, fun(temp_int, gone, citys) + citys[head][temp_int]);
            gone.add(i, temp_int);
        }

        return min == Integer.MAX_VALUE ? citys[head][0] : min;
    }
}

Abu 201,807,061,521,133 cattle off-line code
link: https://www.nowcoder.com/questionTerminal/3d1adf0f16474c90b27a9954b71d125d?orderByHotValue=1&page=1&onlyReference=false
Source: cattle off network
Note the horizontal axis arranged in a matrix form, if it is a 5X5 matrix, The abscissa is
0,121,231,323,123,414 24 124 134 234 1234 34 is
a total of 16

public class Main{
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] map = new int[n][n];
        for(int i=0;i<n;i++) {
            for(int j=0;j<n;j++) {
                map[i][j] = in.nextInt();
            }
        }
        //dp[i][j]表示从i出发,经过j集合里的所有节点一次回到0点的最小小费
        int V = 1<<(n-1);  //
        int[][] dp = new int[n][V];
        for(int i=0;i<n;i++) {     // 先求dp表第一列
            dp[i][0] = map[i][0];   //每个城市回到起点的距离
        }
 
        for(int j=1;j<V;j++) {                  // 再求其他列
            for(int i=0;i<n;i++) {              //从i出发,要去包含j = {010101} 的城市
//                dp[i][j] = 100000;               //或者用 0x7ffff表示无穷大
                dp[i][j] = 0x7ffff;
                if(((j >> (i-1)) & 1) == 1) {    //如果已经到过j了,就continue跳出循环
                    continue;
                }
                for(int k=1;k<n;k++) {          //看能不能先到k城市
                    if(((j >> (k-1)) & 1) == 1) {  //能到
                        dp[i][j] = Math.min(dp[i][j], map[i][k] + dp[k][j ^ (1 << (k-1))]);
                    }
                }
            }
        }
        System.out.println(dp[0][(1 << (n-1))-1]);
 
        /*
         *以下为打印dp表
         */
        System.out.printf("%10d",0);
        for(int j = 0;j < 1 << (n - 1) ;j++){
            System.out.printf("%10d",j);
        }
        System.out.println();
        for(int i = 0;i < n;i++){
            System.out.printf("%10d",i);
            for(int j = 0;j < 1 << (n - 1) ;j++){
                if(dp[i][j] == 0x7ffff) dp[i][j] = -1;
                System.out.printf("%10d",dp[i][j]);
            }
            System.out.println();
        }
 
    }
}

Guess you like

Origin blog.csdn.net/m1f2c3/article/details/99460372