比赛 CSP2022提高组 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 假期计划 最终得分 100
用户昵称 op_组撒头屯 运行时间 2.190 s
代码语言 C++ 内存使用 10.90 MiB
提交时间 2022-10-30 12:16:17
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2500+5;
const int M=10000+5;
const int inf=0x3f3f3f3f;
struct sdf{
	int to,next;
}eg[2*M];
int head[N],ne=0;
int n,m,kk; 
int dis[N][N];
ll w[N];
void add(int x,int y){
	eg[++ne]={y,head[x]};
	head[x]=ne;return ;
}
queue<int>q;
vector<int>v[N];
bool ok[N][N]={0};
void bfs(int s){
	while(!q.empty())q.pop();
	q.push(s);dis[s][s]=0;
	while(!q.empty()){
		int pt=q.front();q.pop();
		for (int i=head[pt];i!=0;i=eg[i].next){
			int v=eg[i].to;
			if (v==s||dis[s][v])continue;
			dis[s][v]=dis[s][pt]+1;
			q.push(v);
		}
	}
	return ;
}
ll mx[5][N];
int mxi[5][N];
bool no[N];
ll calc(int c1,int c2,int x,int y){
	if (c1>2||c2>2)return -1e18;
	int now1=mxi[c1][x],now2=mxi[c2][y];
	if (now1&&now2&&!no[now1]&&!no[now2]&&now1!=now2)return mx[c1][x]+mx[c2][y];
	return max(calc(c1+1,c2,x,y),calc(c1,c2+1,x,y));
}
int main(){
	freopen ("csp2022_holiday.in","r",stdin);
	freopen ("csp2022_holiday.out","w",stdout);
	scanf("%d%d%d",&n,&m,&kk);
	for (int i=2;i<=n;i++)scanf("%lld",&w[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);
		mx[0][i]=mx[1][i]=mx[2][i]=-inf;
		for (int j=1;j<=n;j++){
			if (dis[i][j]&&dis[i][j]<=kk+1){
				v[i].push_back(j);
				ok[i][j]=1;
				if (i==1)continue;
				if (!ok[1][j])continue;
				if (w[j]>=mx[0][i]){
					mx[2][i]=mx[1][i];mx[1][i]=mx[0][i];mx[0][i]=w[j];
					mxi[2][i]=mxi[1][i];mxi[1][i]=mxi[0][i];mxi[0][i]=j;
				}
				else if (w[j]>=mx[1][i]){
					mx[2][i]=mx[1][i];mx[1][i]=w[j];
					mxi[2][i]=mxi[1][i];mxi[1][i]=j;
				}
				else if (w[j]>=mx[2][i]){
					mx[2][i]=w[j];
					mxi[2][i]=j;
				}
			}
		}
	}
	ll ans=0;
	for (int i=2;i<=n;i++){
		for (int j=0;j<v[i].size();j++){
			int x=i,y=v[i][j];
			no[x]=no[y]=1;
			ans=max(ans,calc(0,0,x,y)+w[x]+w[y]);
			no[x]=no[y]=0;
		}
	}
	printf("%lld\n",ans);
	return 0;
}