[Luogu p1433] eat cheese

Portal

Face questions

Title Description

Room stood \ (n \) blocks of cheese. A little mouse should eat them all, at least ask how much distance run? Mice beginning at \ ((0,0) \) at the point.

Input and output formats

Input Format

The first line of a positive integer \ (n-\) . Next, each row (2 \) \ real number, i represents the coordinates of the block of cheese. The distance between the two points of the formula \ (\ sqrt {(x_1-x_2) ^ 2 + (Y_1-Y_2) ^ 2} \) .

Output Format

A number representing the minimum distance run, the reservation \ (2 \) decimal places.

Sample input and output

Input Sample # 1

4
1 1
1 -1
-1 1
-1 -1

Sample Output # 1

7.41

Explanation

\ (1 \ leq n \ leq 15 \) .

analysis

All exposure data range: the problem in a like pressuredp .
This question is like the pressure dpis too simple, I do not want to say ,,

Code

/*
 * @Author: crab-in-the-northeast 
 * @Date: 2020-02-22 23:12:58 
 * @Last Modified by:   crab-in-the-northeast 
 * @Last Modified time: 2020-02-22 23:12:58  
 */
//插件出锅了……Last Modified time应该是2020-02-23 00:58分钟左右,不知道他咋想的,,
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#undef y1

int n;
const int maxn = 25;
double dp[25][40005];

struct cheese {
    double x;
    double y;
}a[maxn];

double getdis(double x1,double y1,double x2,double y2) {
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

double min(double a,double b) {
    return a < b ? a : b;
}

int main() {
    memset(dp,127,sizeof(dp));//初始化dp为无穷大(memset小技巧)
    scanf("%d",&n);
    for(int i = 1; i <= n; i++) scanf("%lf %lf",&a[i].x,&a[i].y);

    for(int s = 1; s <= (1 << n) - 1; s++) {
        for(int i = 1; i <= n; i++) {
            if(s & (1 << (i - 1))) {
                if(s == (1 << (i-1))) {
                        dp[i][s] = 0;
                        continue;
                }
                for(int j = 1; j <= n; j++) {
                    if(i != j && s & (1 << (j - 1))) {
                        dp[i][s] = min(dp[i][s],dp[j][s - (1 << (i - 1))] + getdis(a[i].x,a[i].y,a[j].x,a[j].y));
                    }
                }
            }
        }
    }
    //以上都是状压板子,这里不多说。

    double ans = 1008610086;
    for(int i = 1; i <= n; i++) ans = min(ans,dp[i][(1 << n) - 1]+getdis(a[i].x,a[i].y,a[0].x,a[0].y));
    printf("%.2lf\n",ans);
    return 0;
    //好吧算下来我好想啥都没说……
}

Evaluation results

AC 100: R30954388

Guess you like

Origin www.cnblogs.com/crab-in-the-northeast/p/luogu-p1433.html