跳到正文

个人记录

CSP-S T1 题解

zxh_mc约 7 分钟
原文
文章信息与版本

文章信息

发布
2022年11月8日
收录
2026年7月6日
更新
2026年11月8日
篇幅
约 7 分钟

版本

  1. v1当前

考场上我甚至想到了记录前三大然后分讨,然后以为分类讨论要写一堆如果,然后挂了,打了个暴力。。。

正解

考虑整个路径是由 1,A,B,C,D,11,A,B,C,D,1 个组成,由于 1,A,B1,A,B 和 C,D,1C,D,1 是对称的,所以我们可以考虑枚举 B,CB,C 两个点,然后直接在 O(1)O(1) 内求出 A,DA,D。

做法就是我们记录对于每个点,所有点中与这个点本身距离小于等于 kk,且与 11 的距离也小于等于 kk 的点中权值前 33 大的值。

这样当我们选择好了 B,CB,C 两点之后,只需要枚举 BB,CC 的前三大中不重复的 22 个点权和最大的点,然后与 BB,CC 两点的点权相加,就得出了解。

复杂度

预处理距离是 O(nm)O(nm) 的。

求点权前三大和最后求和都是 O(n2)O(n^2) 的。

所以总复杂度是 O(nm+n2)O(nm+n^2) 的。

AC Code

C++
#include <bits/stdc++.h>

using namespace std;

const int N = 2510, M = 1e4 + 10;
int hd[N], nxt[M * 2], ver[M * 2], idx;
typedef long long LL;

int dis[N][N], n, m, k, d[N][3];
LL s[N], res;

inline void add (int x, int y) {
	ver[++idx] = y;
	nxt[idx] = hd[x];
	hd[x] = idx;
}

void bfs (int st) {
	queue <int> q;
	dis[st][st] = -1;
	q.push(st);
	while (q.size()) {
		int t = q.front(); q.pop();
		for (int i = hd[t]; i ;i = nxt[i]) {
			int y = ver[i];
			if (dis[st][t] + 1 < dis[st][y]) {
				dis[st][y] = dis[st][t] + 1;
				q.push(y);
			}
		}
	}
}

int main () {
//	freopen("holiday.in", "r", stdin);
//	freopen("holiday.out", "w", stdout);
	memset(dis, 0x3f, sizeof(dis));
	s[0] = -0x3f3f3f3f3f3f3f3f;
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 2;i <= n;i++) scanf("%lld", &s[i]);
	for (int i = 1;i <= m;i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		add(x, y); add(y, x);
	}
	for (int i = 1;i <= n;i++) bfs(i);
	for (int i = 2;i <= n;i++) {
		for (int j = 2;j <= n;j++) {
			if (j == i) continue;
			if (dis[j][1] <= k && dis[i][j] <= k) {
				if (s[j] >= s[d[i][0]]) d[i][2] = d[i][1], d[i][1] = d[i][0], d[i][0] = j;
				else if (s[j] >= s[d[i][1]]) d[i][2] = d[i][1], d[i][1] = j;
				else if (s[j] > d[i][2]) d[i][2] = j;
			}
		}
	}
	for (int i = 2;i <= n;i++) {
		for (int j = 2;j <= n;j++) {
			if (i == j || dis[i][j] > k) continue;
			for (int p = 0;p <= 2;p++) {
				for (int q = 0;q <= 2;q++) {
					if (d[i][p] != j && d[i][p] != d[j][q] && d[j][q] != i) {
						if (s[d[i][p]] + s[d[j][q]] + s[i] + s[j] > res) {
							res = s[d[i][p]] + s[d[j][q]] + s[i] + s[j];
						}
					}
				}
			}
		}
	}
	printf("%lld\n", res);
	return 0;
} 

再来一发我的考场 CodeCode(75pts75pts)

C++
#include <bits/stdc++.h>

using namespace std;

const int N = 2510, M = 1e4 + 10;
int hd[N], nxt[M * 2], ver[M * 2], idx;
typedef long long LL;

int dis[N][N], n, m, k;
LL s[N], res;

inline void add (int x, int y) {
	ver[++idx] = y;
	nxt[idx] = hd[x];
	hd[x] = idx;
}

void bfs (int st) {
	queue <int> q;
	dis[st][st] = -1;
	q.push(st);
	while (q.size()) {
		int t = q.front(); q.pop();
		if (dis[st][t] >= k) return;
		for (int i = hd[t]; i ;i = nxt[i]) {
			int y = ver[i];
			if (dis[st][t] + 1 < dis[st][y]) {
				dis[st][y] = dis[st][t] + 1;
				q.push(y);
			}
		}
	}
}

int main () {
//	freopen("holiday.in", "r", stdin);
//	freopen("holiday.out", "w", stdout);
	memset(dis, 0x3f, sizeof(dis));
	scanf("%d%d%d", &n, &m, &k);
	for (int i = 2;i <= n;i++) scanf("%lld", &s[i]);
	for (int i = 1;i <= m;i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		add(x, y); add(y, x);
	}
	for (int i = 1;i <= n;i++) bfs(i);
	for (int j1 = 2;j1 <= n;j1++) {
		if (dis[1][j1] > k) continue;
		for (int j2 = 2;j2 <= n;j2++) {
			if (j2 == j1 || dis[j1][j2] > k) continue;
			for (int j3 = 2;j3 <= n;j3++) {
				if (j3 == j2 || j3 == j1 || dis[j3][j2] > k) continue;
				for (int j4 = 2;j4 <= n;j4++) {
					if (j4 == j1 || j4 == j2 || j4 == j3 || dis[j4][j3] > k || dis[j4][1] > k) continue;
					res = max(res, s[j1] + s[j2] + s[j3] + s[j4]);
				}
			}
		}
	}
	printf("%lld\n", res);
	return 0;
} 
原文
抓取于
  1. _ANIG_

    orz