题解:P12593 沉石鱼惊旋
SekabanTurtle · · 题解
题解:P12593 沉石鱼惊旋
分析题目本质
注意到
搜索方法
暴力找出所有删点方案,然后找到这些方案花费的最小值。
AC code
#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;
}