zoukankan      html  css  js  c++  java
  • CF8C Looking for Order

    原题链接

    • 题意:给(n <= 24)个点,然后给出起点,从起点出发,一个人一次只能拿一个或者两个东西,然后放回起点,求最短路径长度,并输出方案数。
    • 题解:应该是状压dp,但是并没有想清楚,一开始就写了一个假暴力。(dp_s)代表了拿这些物品的最短路径长度,然后应该是(dp_0 = 0)显然,然后应当从已知状态传递到未知状态,设状态为(s), 所以枚举 (i, j),当 (s) 中没有 (i)(j) 的时候,就可以通过 (s) 来传递到 (s | 1 << (i - 1) | 1 << (j - 1)) 的后面状态,然后即 (dp_{s | 1 << (i - 1) | 1 << (j - 1)} = min (dp_{s} + dis(i, j) + dis(i, 0) + dis(j, 0))).
    • 代码:
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <stack>
    #include <vector>
    
    using namespace std;
    typedef long long ll;
    const int N = 33;
    
    int mp[N][N];
    int sx, sy;
    struct node {
        int x, y;
    }a[N];
    int dp[1 << 25];
    int pre[1 << 25];
    int dis(int i, int j) {
        return (a[i].x - a[j].x ) * (a[i].x - a[j].x ) + (a[i].y - a[j].y ) * (a[i].y - a[j].y );
    }
    vector<int>ans;
    void solve() {
        cin >> sx >> sy;
        int n;cin >> n;
        for (int i = 1; i <= n; i ++) cin >> a[i].x >> a[i].y;
        a[0] = {sx, sy};
        int cnt = 0;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i==j)continue;
                mp[i][j] = (a[i].x- a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y);
            }
        }
        int t = 0;
        memset(dp, 0x3f, sizeof dp);
        memset(pre, -1, sizeof pre);
        dp[0] = 0;
        int inf = 0x3f3f3f3f3f3f3f3f;
        for (int s = 0; s < 1 << n; s++) {
            if (dp[s] == inf)continue;
            for (int i = 1; i <= n; i++) {
                if (  s & 1 << (i-1))continue;
                for (int j = 1; j <= n; j++) {
                    if ( s & 1 << (j - 1)) continue;
                    if (dp[s | (1 << (i-1)) | (1 << (j-1))] > dp[s] + dis(j, 0) + dis(i, j) + dis(i, 0)) {
                        dp[s | (1 << (i-1)) | (1 << (j-1))] = dp[s] + dis(j, 0) + dis(i, j) + dis(i, 0);
                        pre[s | (1 << (i-1)) | (1 << (j-1))] = s;
                    }
                }
                break;
            }
        }
        int now = (1 << n) - 1;
        ans.push_back(0);
        while (now != -1) {
            int s = pre[now];
            if (s == -1)s = 0;
            bool f = 1;
            for (int i = 1; i <= n; i++) {
                if ( ((s >> (i-1) ) & 1) != ((now >> (i-1) ) & 1)) {
                    ans.push_back(i);
                    f = 0;
                }
            }
            ans.push_back(0);
            now = s;
            if (s == 0)break;
        }
        cout << dp[ (1 << n)  -1] << endl;
        for (auto iter: ans) cout << iter << " ";
    }
    signed main() {
        int t = 1;
        while (t--) solve();
        return 0;
    }
    
  • 相关阅读:
    php安装扩展的几种方法
    navicat连接linux系统中mysql-错误:10038
    linux下报错bash: service: command not found
    linux配置防火墙和重启防火墙
    linux 环境安装
    匿名函数
    workman的学习总结
    xampp/apache启动失败解决方法
    Linux 查看IP
    慢查询日志
  • 原文地址:https://www.cnblogs.com/Xiao-yan/p/14539934.html
Copyright © 2011-2022 走看看