跳到正文

题解

题解:P12593 沉石鱼惊旋

yanrs111019约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P12593

版本

  1. v1当前

题解:P12593 沉石鱼惊旋

分析题目本质

注意到 1≤n≤81\leq n\leq 8 ,所以直接进行暴力搜索,时间复杂度 O(n!)O(n!) 。

搜索方法

暴力找出所有删点方案,然后找到这些方案花费的最小值。

AC code

C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define N 15
LL n,m,edge[N][N],temp[N][N],ans=LLONG_MAX;
bool vis[N];
vector<LL> vec;

void dfs(LL cnt,LL sum){
    if(sum>=ans)return;
    if(cnt>=n){
        ans=min(ans,sum);
        return;
    }
    for(LL i=1;i<=n;i++){
        if(!vis[i]){
            vector<pair<LL,LL>> de;
            LL c=0,s=0;
            for(LL j=1;j<=n;j++){
                if(temp[i][j]){
                    s+=temp[i][j];
                    c++;
                    de.emplace_back(j,temp[i][j]);
                }
            }
            for(auto& p:de){
                LL j=p.first;
                temp[i][j]=0;
                temp[j][i]=0;
            }
            vis[i]=1;
            vec.push_back(i);
            dfs(cnt+1,sum+c*s);
            vec.pop_back();
            vis[i]=0;
            for(auto& p:de){
                LL j=p.first;
                LL w=p.second;
                temp[i][j]=w;
                temp[j][i]=w;
            }
        }
    }
}

int main(){
    LL x,y,z;
    scanf("%lld%lld",&n,&m);
    for(LL i=1;i<=m;i++){
        scanf("%lld%lld%lld",&x,&y,&z);
        edge[x][y]=edge[y][x]=z;
        temp[x][y]=temp[y][x]=z;
    }
    dfs(0,0);
    printf("%lld",ans);
    return 0;
}
原文