[NOIP2017] Treasure

Topic Link

The meaning of problems

  Face problems too long, self-transfer ...

analysis

  At first glance pruning is a search, but is solution-like positive pressure DP

  We set may reach a point in binary, set $ d [i] [s] $ represents the shortest path to the point $ I $ length from point set $ s $, $ f [i] [s] $ denotes Add $ I $ depth point after point set $ s $ required minimum cost (depth points to the starting point after the minimum)

  First, an enumeration process $ s $ $ $ D array, and then is sequentially updated in accordance with the depth of $ f $ array, the depth of each enumeration $ s $, whichever directly to a subset of all of its minimum cost, the final answer

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define ll long long
#define inf 6000000
#define N 13

int n, m, u, v, w, all;
int g[N][N], f[N][1 << N], d[N][1 << N];

int main() {
    scanf("%d%d", &n, &m);
    all = (1 << n) - 1;
    for (int i = 1; i <= n; i++)
        for (int j = 0; j <= all; j++)
            d[i][j] = f[i][j] = inf;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            g[i][j] = inf;
    for (int i = 1; i <= n; i++) g[i][i] = 0;
    for (int i = 1; i <= m; i++) {
        scanf("%d%d%d", &u, &v, &w);
        g[u][v] = g[v][u] = min(g[u][v], w);
    }
    for (int s = 0; s <= all; s++)
        for (int i = 1; i <= n; i++)
            if ((1 << (i - 1)) & s)
                for (int j = 1; j <= n; j++)
                    if (!((1 << (j - 1)) & s))
                        d[j][s] = min(d[j][s], g[i][j]);
    for (int i = 1; i <= n; i++) f[1][1 << (i - 1)] = 0;
    for (int i = 2; i <= n; i++)
        for (int s = 0; s <= all; s++)
            for (int k = s; k; k = (k - 1) & s) {
                int tot = 0;
                for (int j = 1; j <= n; j++)
                    if ((1 << (j - 1)) & (k ^ s))
                        tot += d[j][k];
                f[i][s] = min(f[i][s], f[i - 1][k] + (i - 1) * tot);
            }
    printf("%d\n", f[n][all]);

    return 0;
}
View Code

Guess you like

Origin www.cnblogs.com/Pedesis/p/10991150.html