zoukankan      html  css  js  c++  java
  • UVA

    题目大意:有 2 * n 个点,使其组成 n 对。求 n 对点集的最小距离之和.

    解题思路:因为 2 * n 最大才 20,全然能够由数字的位来表示。每个数字表示一个状态,然后才去记忆化搜索的方式得出结果。



    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    
    int N;
    double DP[1 << 17], dis[20][20];
    
    double cal(int x, int y) {
        return sqrt(x * x + y * y);
    }
    
    double DPS(int S) {
        if (DP[S] || !S)
            return DP[S]; 
        int i;
        for (i = N - 1; i >= 0; i--)
            if (S & (1 << i)) break;
        DP[S] = 0x3f3f3f3f;
        for (int j = i - 1; j >= 0; j--)
            if (S & (1 << j))
                DP[S] = min(DP[S], dis[i][j] + DPS(S ^ (1 << i) ^ (1 << j)));
        return DP[S];
    }
    
    int main() {
        int x[20], y[20], kase = 0;
        while (scanf("%d", &N), N) {
            N <<= 1;
            for (int i = 0; i < N; i++)
                scanf("%*s%d%d", &x[i], &y[i]);
    
            for (int i = 0; i < N; i++)
                for (int j = i + 1; j < N; j++)
                    dis[j][i] = cal(x[i] - x[j], y[i] - y[j]);
            memset(DP, 0, sizeof(DP));
            printf("Case %d: %.2lf
    ", ++kase, DPS((1 << N) - 1));
        }
        return 0;
    }
    
  • 相关阅读:
    yii2框架安装
    RabbitMq简单应用
    PHP扩展开发--编写一个helloWorld扩展
    node 笔记整理
    js 笔记整理
    JavaScript event loop事件循环 macrotask与microtask
    移动端 缩放插件备份
    vue 笔记备份
    echart 打开新世界的大门
    canvas 笔记整理
  • 原文地址:https://www.cnblogs.com/tlnshuju/p/7221626.html
Copyright © 2011-2022 走看看