跳到正文

题解

P14362 [CSP-S 2025] 道路修复 题解

lyrgo约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年11月2日
收录
2026年7月24日
更新
2026年10月26日
篇幅
约 4 分钟
题目
P14362

版本

  1. v1当前

题目大意

给定 nn 个点,mm 条边的无向图且有边权,有 kk 个额外点,每个额外点向这 nn 个点连边,且额外点有点权,求最小生成树。 n≤1e4n\leq 1e4,m≤1e6m\leq 1e6,k≤10k\leq 10。

Sol

从考场思路改了一点。

先考虑 k≤0k\leq 0 部分分,就是直接跑最小生成树,能获得 1616 分。 特殊性质 A 中,如果点权为 00 且存在一条边权为 00,可以直接从边权为 00 所连点向所有点连边,再跑最小生成树即可。

看看 kk 的范围很小,且比较麻烦的点在于处理它独特的点权,不难想到直接枚举某个点选和不选的子集,这样先把点权代价加上,再将所选额外点的连边加上跑最小生成树就可以了,复杂度 O(2k(m+k)log⁡(m+k))O(2^k(m+k)\log (m+k)),仍然无法通过这一题。

复杂度瓶颈在于每次枚举子集都要排序,原图的 mm 个边只有 n−1n-1 个有效,把它们存下来,和 nknk 个额外点连边一起预处理排序,做最小生成树时判断所连点在不在选中范围内就好了。
预处理 n−1n-1 条边是 O(mlog⁡m)O(m\log m) 的,最后处理复杂度为 O((n+nk)log⁡(n+nk))O((n+nk)\log (n+nk)),足以通过本题。

Code

代码
C++
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

typedef long long LL;
struct Node {int u , v; LL w;};

const int N = 1e4+10 , M = 1e6+10 , K = 15;

int n , m , k;
Node tr[M];
vector<Node> vec;
LL ct[K];
int p[N];

int find(int x) {
	if(x != p[x]) p[x] = find(p[x]);
	return p[x];
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	
	cin >> n >> m >> k;
	for(int i = 1 ; i <= m ; i ++)
		cin >> tr[i].u >> tr[i].v >> tr[i].w;
	
	for(int i = 1 ; i <= n ; i ++)
		p[i] = i;
	sort(tr+1 , tr+m+1 , [](const Node& a , const Node& b) {
		return a.w < b.w;
	});
	int block = n;
	for(int i = 1 ; i <= m && block > 1 ; i ++) {
		if(find(tr[i].u) == find(tr[i].v)) continue;
		vec.emplace_back(tr[i]);
		p[find(tr[i].u)] = find(tr[i].v);
		block --;
	}
	
	for(int i = 1 ; i <= k ; i ++) {
		cin >> ct[i];
		for(int j = 1 ; j <= n ; j ++) {
			LL x; cin >> x;
			vec.push_back({n+i , j , x});
		}
	}
	
	sort(vec.begin() , vec.end() , [](const Node& a , const Node& b) {
		return a.w < b.w;
	});
	
	LL res = 0x3f3f3f3f3f3f3f3f;
	for(int state = 0 ; state < (1<<k) ; state ++) {
		LL tmpres = 0;
		for(int j = 0 ; j < k ; j ++) {
			if(state & (1 << j)) tmpres += ct[j+1];
		}
		
		for(int i = 1 ; i <= n+k ; i ++)
			p[i] = i;
		block = n;
		for(auto i : vec) {
			if(i.u > n && (state&(1<<(i.u-n-1))) == 0) continue;
			if(find(i.u) == find(i.v)) continue;
			
			tmpres += i.w;
			p[find(i.u)] = find(i.v);
			if(i.u <= n) block --;
			if(block == 1) break;
		}
		res = min(res , tmpres);
	}
	cout << res << '\n';
	return 0;
}
原文