跳到正文

题解

[CSP-S 2025] 道路修复 / road

Eclatara约 17 分钟
原文
文章信息与版本

文章信息

发布
2025年11月2日
收录
2026年8月5日
更新
2026年11月14日
篇幅
约 17 分钟
获赞
22
题目
P14362

版本

  1. v1当前

盐城机器给我主机卡死机了,最后重写没跳出来

当 K=0K=0 的时候就是最小生成树的板子,预计得分 16pts16pts。

下面我们考虑 K>0K > 0 的情况:

我们很难不注意到 KK 的范围真的很小,于是大胆的猜测和 2K2^K 强相关。

于是我们枚举子集,对于每一次暴力的建图跑最小生成树。 预计得分: 64pts64pts。

我们不难的发现,每次建图跑的时候会有很多重复的节点,所以我们就不需要每次重新建图,通过改变枚举子集的方式,对于边的集合显然可以由上一个加上菊花图得来。预计得分: 80pts80pts。

无论如何,我们不难发现每次建图真的很反人类。我们考虑能不能一次建好。

于是我们可以参考克鲁斯卡尔的过程,直接建立好图,每次只需要加上一个判断是否枚举选用村庄的时候选用了。于是我们得到了 O(2k(m+kn)+(m+kn)log⁡(m+kn))O(2^k (m+kn) + (m+kn) \log (m+kn)),由于有并查集,所以常数较大。预计得分 80pts∼100pts80pts \sim 100pts。

C++
#include<bits/stdc++.h>
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
int n, m, k;
const int N = 10100, M = 2e6 + 10; 
int a[11][N], c[11];
struct node {
	int u, v, w;
} e[M];
bool cmp(node x, node y) {
	return x.w < y.w;
}
bool h[11];  
int fa[N + 11];  
int find(int x) {
	if (fa[x] == x) return x;
	return fa[x] = find(fa[x]);
}
long long kruskal(int exn, const int E) {
	for (int i = 1; i <= n + k; ++i) fa[i] = i;  
	long long vos = 0;
	int cnt = 0;
	for (int i = 1; i <= E; ++i) {
		node x = e[i];
		if ((x.u > n && !h[x.u - n]) || (x.v > n && !h[x.v - n])) {
			continue;
		}
		int fu = find(x.u), fv = find(x.v);
        if(fu==fv) continue;
		fa[fv] = fu;
		vos += x.w;
		cnt++;
        if(cnt==n+exn-1) break;
	}
	return vos;  
}
int main() {
	cin >> n >> m >> k;
	for (int i = 1; i <= m; ++i) {
		int u, v, w;
		cin >> u >> v >> w;
		e[i] = {u, v, w};
	}
	long long E = m;
	for (int i = 1; i <= k; ++i) {
		cin >> c[i];
		for (int j = 1; j <= n; ++j) {
			cin >> a[i][j];
			e[++E] = {n + i, j, a[i][j]};  
		}
	}
	long long ans = LLONG_MAX;
	sort(e + 1, e + E + 1, cmp);
	for (int mask = 0; mask < (1 << k); ++mask) {
		memset(h, 0, sizeof(h));
		long long cost = 0, cnt = 0;
		for (int j = 1; j <= k; ++j) {
			if (mask & (1 << (j - 1))) {
				cost += c[j];
				h[j] = 1; 
				cnt++;
			}
		}
		ans = min(ans, cost + kruskal(cnt, E)); 
	}
	cout << ans;
}

我真的不明白为啥能跑那么快,最大的点居然没到 1s1s 。

我们考虑继续优化,很明显原图只需要保留最小生成树即可。并不需要把所有的节点都加进去,于是我们加上一个最好生成树的板子放上面即可。时间复杂度 O(2kkn+mlog⁡m)O(2^k kn + m \log m )。

但是我不是很明白为啥跑到还没上一个快??

C++
#include<bits/stdc++.h>
using namespace std;
namespace fast_IO {
#define IOSIZE 100000
	char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
	template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
	template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
	template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
	inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
	inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
	inline void print(char x) { putchar(x); }
	inline void print(char *x) { while (*x) putchar(*x++); }
	inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
	inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
	inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
	inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
	inline void print(bool b) { putchar(b+48); }
	template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
	template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
	struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
	template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
	template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
int n, m, k;
const int N = 10100, M = 2e6 + 10; 
int a[11][N], c[11];
struct node {
	int u, v, w;
} e[M],p[M];
bool cmp(node x, node y) {
	return x.w < y.w;
}
bool h[11];  
int fa[N + 11];  
int find(int x) {
	if (fa[x] == x) return x;
	return fa[x] = find(fa[x]);
}
inline long long kruskal(int exn, const int E) {
	for (int i = 1; i <= n + k; ++i) fa[i] = i;  
	long long vos = 0;
	int cnt = 0;
	for (int i = 1; i <= E; ++i) {
		node x = e[i];
		if ((x.u > n && !h[x.u - n]) || (x.v > n && !h[x.v - n])) {
			continue;
		}
		int fu = find(x.u), fv = find(x.v);
		if (fu == fv) continue;
		fa[fv] = fu;
		vos += x.w;
		cnt++;
		if(cnt==n+exn-1) break;	
	}
	return vos;  
}
long long E;
inline void kruskal2() {
	sort(p + 1, p + m + 1, cmp);
	for (int i = 1; i <= n; ++i) fa[i] = i;  
	int cnt = 0;
	for (int i = 1; i <= m; ++i) {
		node x = p[i];
		int fu = find(x.u), fv = find(x.v);
		e[++E]=x;
		if (fu == fv)  continue;
		fa[fv] = fu;
		cnt++;
		if(cnt==n-1) break;	
	}
}
int main(){
	cin >> n >> m >> k;
	for (int i = 1; i <= m; ++i) {
		int u, v, w;
		cin>>u>>v>>w;
		p[i] = {u, v, w};
	}
	kruskal2();
	for (int i = 1; i <= k; ++i) {
		cin >> c[i];
		for (int j = 1; j <= n; ++j) {
			cin >> a[i][j];
			e[++E] = {n + i, j, a[i][j]};  
		}
	}
	long long ans = LLONG_MAX;
	sort(e + 1, e + E + 1, cmp);
	for (int mask = 0; mask < (1 << k); ++mask) {
		memset(h, 0, sizeof(h));
		long long cost = 0, cnt = 0;
		for (int j = 1; j <= k; ++j) {
			if (mask & (1 << (j - 1))) {
				cost += c[j];
				h[j] = 1; 
				cnt++;
			}
		}
		ans = min(ans, cost + kruskal(cnt, E)); 
	}
	cout << ans;
}

大概率可以进行 primprim 堆优化从而达到 O(2kkn+mlog⁡m)O(2^k kn + m \log m)。

此外,这题存在 O(2k(n+k))O(2^k (n+k)) 的做法。

原文
存档评论16 条抓取于