题解:P12593 沉石鱼惊旋

· · 题解

观察到 n 非常小,令 a_n 表示删点顺序的序列,枚举全排列,每次先枚举 a 再枚举别的点,若还有边就把 k 和 tot 加上,直接算答案统计最小值即可。

这里存图使用了邻接矩阵,枚举全排列使用了 STL 中的 next_permutation,还有就是注意每次把边删了记得加回来。

int a[maxn], ans = 1e18, G[10][10], G1[10][10]; 
inline void solve() {
    std::cin >> n >> m;
    for(int i = 1, u, v, w; i <= m; i++){
        std::cin >> u >> v >> w;
        G1[u][v] = w;
        G1[v][u] = w;
    }
    for(int i = 1; i <= n; i++){
        a[i] = i;
    }
    do{
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= n; j++){
                G[i][j] = G1[i][j];
            }
        }
        int now = 0;
        for(int i = 1; i <= n; i++){
            int k = 0, tot = 0;
            for(int j = 1; j <= n; j++){
                if(j == a[i]) continue;
                if(G[j][a[i]]){
                    tot += G[j][a[i]];
                    k++;
                    G[j][a[i]] = G[a[i]][j] = 0;
                }
            }
            now += tot * k;
        }
        ans = std::min(ans, now);
    }while(std::next_permutation(a + 1, a + n + 1));
    std::cout << ans << '\n';
}