12.26 总结

· · 闲话

T1 P14362 [CSP-S 2025] 道路修复

骗分没骗到。。。

两个瞎写调试的代码忘记删掉了,导致 0 分。

正解:

因为 k \le 10,所以可以全排列每一个可能的答案。

全排列基础代码:

inline void dfs(int x,int y){
    if(x>k){
//      TODO
    }
    v[x]=1;
    dfs(x+1,y+a[x]);
    v[x]=0;
    dfs(x+1,y);
}

接下来就可以在选完 k 个点之后跑一遍最小生成树(此刻一共变成了 num+n 个点)。

:::success[AC Code]{open}


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll N=1e7+20;
ll n,m,k;
ll p[N],b,c[N];
ll cnt,ans=1e18;
bool vis[N];
struct node{
    ll x,y,z;
}a[N];
inline bool cmp(node x,node y){
    return x.z<y.z;
}
inline ll find(ll x){
    if(p[x]==x) return x;
    return p[x]=find(p[x]);
}
inline void dfs(ll u,ll num,ll cost){
    if(u>k){
        ll sum=cost;
        for(int i=1;i<=n+k+1;++i) p[i]=i;
        cnt=0;
        num+=n;
        for(int i=1;i<=m;++i){
            ll x=a[i].x,y=a[i].y,z=a[i].z;
            ll fx=find(x),fy=find(y);
            if(x>n&&vis[x-n]==0) continue;
            if(fx!=fy){
                p[fx]=p[fy];
                ++cnt;
                sum+=z;
                if(cnt==num-1){
                    break;
                }
            }
        }
        ans=min(ans,sum);
        return;
    }
    vis[u]=1;
    dfs(u+1,num+1,cost+c[u]);
    vis[u]=0;
    dfs(u+1,num,cost);
}
int main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m>>k;
    for(ll i=1;i<=m;++i)
        cin>>a[i].x>>a[i].y>>a[i].z;
    for(int i=n+1;i<=n+k;++i){
        cin>>c[i-n];
        for(int j=1;j<=n;++j){
            cin>>b;
            a[++m]={i,j,b};
        }
    }
    sort(a+1,a+m+1,cmp);
    dfs(1,0,0);
    cout<<ans;
    return 0;
}
:::