| #include <cstdio> |
| #include <vector> |
| #include <queue> |
| #include <algorithm> |
| using namespace std; |
| typedef long long LL; |
| const int N = 2505; |
| vector<int> G[N]; |
| int dis[N], top3[N][3]; |
| bool vis[N], ok[N][N]; |
| LL s[N]; |
| int main() |
| { |
| int n, m, k; |
| scanf("%d%d%d", &n, &m, &k); |
| for (int i = 2; i <= n; i++) scanf("%lld", &s[i]); |
| for (int i = 0; i < m; i++) { |
| int x, y; scanf("%d%d", &x, &y); |
| G[x].push_back(y); G[y].push_back(x); |
| } |
| queue<int> q; |
| for (int i = 1; i <= n; i++) { |
| while (!q.empty()) q.pop(); |
| for (int j = 1; j <= n; j++) { |
| dis[j] = -1; vis[j] = false; |
| } |
| q.push(i); vis[i] = true; dis[i] = 0; |
| while (!q.empty()) { |
| int cur = q.front(); q.pop(); |
| for (int to : G[cur]) { |
| if (!vis[to]) { |
| vis[to] = true; |
| dis[to] = dis[cur] + 1; |
| if (dis[to] <= k + 1) { |
| q.push(to); ok[i][to] = true; |
| } |
| } |
| } |
| } |
| } |
| for (int i = 2; i <= n; i++) { |
| for (int j = 2; j <= n; j++) { |
| |
| if (ok[1][j] && ok[i][j]) { |
| if (s[j] > s[top3[i][0]]) { |
| top3[i][2] = top3[i][1]; |
| top3[i][1] = top3[i][0]; |
| top3[i][0] = j; |
| } else if (s[j] > s[top3[i][1]]) { |
| top3[i][2] = top3[i][1]; |
| top3[i][1] = j; |
| } else if (s[j] > s[top3[i][2]]) { |
| top3[i][2] = j; |
| } |
| } |
| } |
| } |
| LL ans = 0; |
| for (int b = 2; b <= n; b++) { |
| for (int c = 2; c <= n; c++) { |
| if (b == c || !ok[b][c]) continue; |
| for (int i = 0; i < 3; i++) { |
| int a = top3[b][i]; |
| if (a == 0) break; |
| if (a == b || a == c) continue; |
| for (int j = 0; j < 3; j++) { |
| int d = top3[c][j]; |
| if (d == 0) break; |
| if (d == a || d == b || d == c) continue; |
| ans = max(ans, s[a] + s[b] + s[c] + s[d]); |
| } |
| } |
| } |
| } |
| printf("%lld\n", ans); |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?