zoukankan      html  css  js  c++  java
  • HNUSTOJ-1621 Picking Cabbage(状态压缩DP)

    1621: Picking Cabbage

    时间限制: 2 Sec  内存限制: 32 MB
    提交: 26  解决: 14
    [提交][状态][讨论版]

    题目描述

    Once, Doraemon and  Nobita planted a farm with cabbage. One night their farm was stealed by Takeshi Gian. Takeshi Gian picked away most of the cabbage, but left some cabbage in the farm. Then he left a note to Doraemon and  Nobita, telling them the coordinate of the cabbage still in the farm. As soon as Doraemon and  Nobita get the note, they run out to save their cabbage.

    Doraemon has a warp gate in his house that can send them to a cabbage which they wanted to. Then they should run from one cabbage to another to get them. Since they wanted to get all the cabbage as soon as possible, they should run the shortest way. Can you calculate the shortest path that they should run

    输入

    There are multiple test cases. The first line is a positive integer stands for the number of test cases.
    The first line of each test case is a positive integer N(1<=N<=15) stands for the number of cabbage that Takeshi Gian left.
    Then N following lines each has two integer xi, yi, (0<=xi,yi<=100) stands for the coordinate of the cabbage.

    输出

    Output the shortest path of getting all the cabbage in one line keeping two decimal places.
    Doraemon and  Nobita just wanted to get all of the cabbage as soon as possible, so, don't bother about their path of getting back home.

    样例输入

    2
    3
    0 0
    0 1
    0 2
    3
    0 0
    0 1
    1 0

    样例输出

    2.00
    2.00
    状态压缩DP
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    
    using namespace std;
    const int N = 15;
    const int M = (1<<15);
    const double INF = (1<<20);
    double dp[N][M];
    int x[N], y[N], n, u;
    
    
    inline double dist(int v, int i){
        return sqrt((x[v] - x[i]) * (x[v] - x[i]) + (y[v] - y[i]) * (y[v] - y[i]));
    }
    
    double DP(int v, int S){
        if(dp[v][S]) return dp[v][S];
        auto M = INF;
    
        S |= (1 << v);
        for(int i = 0; i < n; ++ i)
            if( !(S & (1 << i)))
                M = min(M, DP(i, S) + dist(v, i));
        S &= (~(1 << v));
    
        return dp[v][S] = (M != INF? M : 0);
    }
    
    int main(){
        int T;
        scanf("%d", &T);
        while(T --){
            memset(dp, 0, sizeof(dp));
            scanf("%d", &n);
            for(int i = 0; i < n; ++ i){
                scanf("%d %d", &x[i], &y[i]);
            }
            auto Min = INF;
            for(int i = 0; i < n; ++ i){
                Min = min(Min, DP(i, 0));
            }
            printf("%.2f
    ", Min);
        }
        return 0;
    }
     
  • 相关阅读:
    win7一关机电脑就蓝屏
    win10系统怎么查自己电脑的IP地址[系统天地]
    Win7如何设置任务管理器的快捷键?[系统天地]
    Win10系统下安全登录密码忘记了怎么办【系统天地】
    Win7系统很卡应该如何解决?[系统天地]
    Win7系统运行慢如何解决?——系统天地
    Win7如何解决开机应用程序无法正常启动
    win10系统怎么切换独立显卡
    程序员的自我修养
    SQL学习笔记
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7458316.html
Copyright © 2011-2022 走看看