P8817 [CSP-S 2022] 假期计划 C++ 题解
考场上磕了一个多钟,并没有做出来,但我仍然认为是绿题。
属于是今年 CSP 的一大遗憾了。
题意
给定一个
-
- 每一段路径上经过的点的数量小于等于
k 。
思路
如果想到枚举
先
定义“一步可以走到”指这两个点之间经过的点小于等于
枚举了
因此,对于每个点
接下来暴力枚举满足条件的 而且极其跑不满啊。
记得开上点权的 long long。
#include <bits/stdc++.h>
#define rep1(i, l, r) for (int i = l; i <= int(r); ++i)
#define rep2(i, l, r) for (int i = l; i >= int(r); --i)
#define fst first
#define snd second
#define mp make_pair
#define eb emplace_back
#define ptc putchar
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pll;
const int MAXN = 2.5e3 + 10, INF = ~0U >> 1, inf = ~0U >> 2;
namespace stupid_lrc {
};
using namespace stupid_lrc;
int n, m, k, d[MAXN][MAXN]; ll w[MAXN], ans;
bool vis[MAXN]; vector <int> lnk[MAXN];
set <pll> bs[MAXN]; // 1 与 i 都能一步到达的点
il void solve(int s, int *d) {
queue <int> q; memset(vis, 0, sizeof vis);
rep1(i, 1, n) d[i] = INF;
d[s] = 0; q.push(s);
while (q.size()) {
int now = q.front(); q.pop();
if (vis[now]) continue;
vis[now] = true;
for (auto v : lnk[now]) if (d[v] > d[now] + 1) d[v] = d[now] + 1, q.push(v);
}
}
int main() {
read(n, m, k); rep1(i, 2, n) read(w[i]);
rep1(i, 1, m) {
int u, v; read(u, v);
lnk[u].eb(v); lnk[v].eb(u);
}
rep1(i, 1, n) solve(i, d[i]);
rep1(i, 2, n) rep1(j, 2, n) {
if (j == i) continue;
if (d[i][j] <= k + 1 && d[1][j] <= k + 1) bs[i].insert(mp(w[j], j));
if (bs[i].size() > 3) bs[i].erase(bs[i].begin());
}
rep1(b, 2, n) rep1(c, 2, n) {
if (d[b][c] > k + 1 || b == c) continue;
for (auto a : bs[b]){
if (a.snd == b || a.snd == c) continue;
for (auto d : bs[c]) {
if (d.snd == b || d.snd == c || d.snd == a.snd) continue;
ans = max(ans, w[b] + w[c] + w[a.snd] + w[d.snd]);
}
}
} cout << ans << endl;
return 0;
}
最大点 550ms 以内。
在这个结尾,不要脸地放上我 CSP2022 的思考与汗泪。