跳到正文

闲话

12.26 总结

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

文章信息

发布
2026年1月1日
收录
2026年1月5日
更新
2026年11月15日
篇幅
约 3 分钟

版本

  1. v1当前

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

骗分没骗到。。。

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

  • cnt==n →\rightarrow cnt==n-1:16 pts16\ pts。

  • 还有一个忘了。

正解:

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

全排列基础代码:

C++
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);
}

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

AC Code
C++
#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;
}
原文