比赛 CSP2022提高组 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 假期计划 最终得分 100
用户昵称 yrtiop 运行时间 1.703 s
代码语言 C++ 内存使用 8.98 MiB
提交时间 2022-10-30 09:54:33
显示代码纯文本
#include <bits/stdc++.h>
using i64 = long long;

const int maxn = 2505;
const int maxm = 2e4 + 5;
int head[maxn],ver[maxm],nxt[maxm],cnt;
int n,m,k,Q[maxn],h,t,dis[maxn][maxn],QAQ[maxn][3];
i64 s[maxn],d[maxn][3];
bool vis[maxn];

void add(int u,int v) {
	ver[++ cnt] = v;
	nxt[cnt] = head[u];
	head[u] = cnt;
	return ;
}

int main() {
	freopen("csp2022_holiday.in","r",stdin);
	freopen("csp2022_holiday.out","w",stdout);
	scanf("%d %d %d",&n,&m,&k);
	for(int i = 2;i <= n;++ i)scanf("%lld",&s[i]);
	++ k;
	for(int i = 1;i <= m;++ i) {
		int u,v;
		scanf("%d %d",&u,&v);
		add(u , v);
		add(v , u);
	}
	for(int i = 1;i <= n;++ i) {
		for(int j = 1;j <= n;++ j)
			dis[i][j] = 0x3f3f3f3f,vis[j] = false;
		Q[h = t = 1] = i;
		dis[i][i] = 0;
		vis[i] = true;
		while(h <= t) {
			int u = Q[h ++];
			for(int p = head[u];p;p = nxt[p]) {
				int v = ver[p];
				if(vis[v])continue ;
				dis[i][v] = dis[i][u] + 1;
				Q[++ t] = v;
				vis[v] = true;
			}
		}
	}
	
	for(int i = 2;i <= n;++ i) {
		for(int j = 2;j <= n;++ j) {
			if(dis[1][j] > k||dis[j][i] > k||i == j)continue ;
			if(s[j] > d[i][0]) {
				d[i][2] = d[i][1];
				d[i][1] = d[i][0];
				d[i][0] = s[j];
				QAQ[i][2] = QAQ[i][1];
				QAQ[i][1] = QAQ[i][0];
				QAQ[i][0] = j;
			}
			else if(s[j] > d[i][1]) {
				d[i][2] = d[i][1];
				d[i][1] = s[j];
				QAQ[i][2] = QAQ[i][1];
				QAQ[i][1] = j;
			}
			else if(s[j] > d[i][2]) {
				d[i][2] = s[j];
				QAQ[i][2] = j;
			}
		}
	}
	
	i64 ans = 0;
	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 < 3;++ p) {
				if(QAQ[i][p]&&QAQ[i][p] != j) {
					for(int q = 0;q < 3;++ q) {
						if(QAQ[j][q]&&QAQ[j][q] != QAQ[i][p]&&QAQ[j][q] != i) {
							ans = std::max(ans , d[i][p] + d[j][q] + s[i] + s[j]);
						}
					}
				}
			}
		}
	}
	
	printf("%lld",ans);
	return 0;
}