思路抽象,望有大佬求调QAQ WA #1, 5, 21, 31 TLE #18, 19, 20, 25, 26, 44, 45, 46
#include<bits/stdc++.h>
#define int long long
using namespace std;
struct edge{
int l, r, useable;
};
struct node{
int ax[3] = {-1, -1, -1};
int id[3] = {-1, -1, -1};
int get_num(int a){
return ax[a];
};
int get_id(int a){
return id[a];
};
void insert(int a, int b){
for(int i = 0; i < 3; i++){
if(b == id[i]){
return;
}
if(a > ax[i]){
for(int j = 2; j >= j + 1; j--){
ax[j] = ax[j - 1];
id[j] = id[j - 1];
}
id[i] = b;
ax[i] = a;
return;
}
}
};
};
vector<edge> alle;
vector<int> nodes[3000];
node abc[3000];
int val[3000], ans = 0;
int n, m, k, l, r, ecn, vis[3000], depth[3000], reach1[3000], fir[3000][3000];
void add_edge(int ll, int rr, int usea){
alle.push_back((edge){ll, rr, usea});
nodes[ll].push_back(ecn);
ecn++;
alle.push_back((edge){rr, ll, usea});
nodes[rr].push_back(ecn);
ecn++;
}
void bfs(int st){
queue<int> q;
q.push(st);
vis[st] = st;
depth[st] = 0;
while(q.size()){
int now_node = q.front();
q.pop();
for(int i = 0; i < nodes[now_node].size(); i++){
edge e = alle[nodes[now_node][i]];
if(vis[e.r] != st && e.useable){
depth[e.r] = depth[now_node] + 1;
q.push(e.r);
vis[e.r] = st;
if(st == 1 && depth[e.r] <= k + 1){
reach1[e.r] = 1;
}
if(depth[e.r] <= k + 1 && !fir[st][e.r]){
add_edge(st, e.r, 0);
fir[st][e.r] = fir[e.r][st] = 1;
}
}
}
}
}
void cal(int x){
for(int i = 0; i < nodes[x].size(); i++){
edge e = alle[nodes[x][i]];
if(reach1[e.r]){
abc[x].insert(val[e.r], e.r);
}
}
}
signed main(){
clock_t st = clock();
cin >> n >> m >> k;
for(int i = 2; i <= n; i++){
cin >> val[i];
}
for(int i = 1; i <= m; i++){
cin >> l >> r;
add_edge(l, r, 1);
fir[l][r] = 1;
fir[r][l] = 1;
}
for(int i = 1; i <= n; i++){
bfs(i);
}
for(int i = 2; i <= n; i++){
cal(i);
}
for(int i = 2; i <= n; i++){
for(int j = 2; j <= n; j++){
if((double)(clock() - st) / CLOCKS_PER_SEC >= 2.1){
break;
}
if(!fir[i][j]){
continue;
}
if(i != j){
for(int kk = 0; kk < 3; kk++){
int id1 = abc[i].get_id(kk);
if(id1 == -1 || id1 == j){
continue;
}
for(int ll = 0; ll < 3; ll++){
int id2 = abc[j].get_id(ll);
if(id2 == -1 || id2 == id1 || id2 == i){
continue;
}
ans = max(ans, val[i] + val[j] + abc[j].get_num(ll) + abc[i].get_num(kk));
}
}
}
}
}
cout << ans;
}