mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-10-17 21:42:25 +00:00
refactor(P8817): 重构题目解法并优化性能
将原递归实现改为BFS算法计算节点间距离 使用预处理和剪枝优化计算效率 添加完整解题逻辑计算最大得分
This commit is contained in:
parent
d3c7ed51a5
commit
66f957900f
@ -1,59 +0,0 @@
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
using ll = int64_t;
|
||||
ll n,m,k;
|
||||
std::vector<ll> score;
|
||||
std::vector<std::vector<ll>> edg;
|
||||
std::vector<std::vector<bool>> cango;
|
||||
std::vector<std::vector<ll>> best3;
|
||||
|
||||
void dfscango(ll from,ll cur, ll curk){
|
||||
if(curk>k){
|
||||
return;
|
||||
}
|
||||
for(ll i=0;i<edg[cur].size();i++){
|
||||
cango[from][edg[cur][i]]=true;
|
||||
dfscango(from, edg[cur][i], curk+1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::cin>>n>>m>>k;
|
||||
score.resize(n+1);
|
||||
edg.resize(n+1);
|
||||
cango.resize(n+1,std::vector<bool>(n+1));
|
||||
best3.resize(n+1);
|
||||
for(ll i=2;i<=n;i++){
|
||||
std::cin>>score[i];
|
||||
}
|
||||
for(ll i=1;i<=m;i++){
|
||||
ll u,v;
|
||||
std::cin>>u>>v;
|
||||
edg[u].push_back(v);
|
||||
edg[v].push_back(u);
|
||||
}
|
||||
for(ll i=1;i<=n;i++){
|
||||
dfscango(i, i, 0);
|
||||
}
|
||||
for(ll i=2;i<=n;i++){
|
||||
std::priority_queue<std::pair<ll, ll>,std::vector<std::pair<ll, ll>>,std::greater<>> pr;
|
||||
for(ll j=2;j<=n;j++){
|
||||
if(i==j)continue;
|
||||
if(cango[i][j]&&cango[j][1]){
|
||||
pr.emplace(score[j],j);
|
||||
}
|
||||
}
|
||||
//TODO
|
||||
}
|
||||
for(ll B=2;B<=n;B++){
|
||||
for(ll C=B+1;C<=n;C++){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
99
src/9/25/P8817.cpp
Normal file
99
src/9/25/P8817.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
|
||||
using ll = long long;
|
||||
|
||||
const int MAXN = 2505;
|
||||
|
||||
int n;
|
||||
int m;
|
||||
int k;
|
||||
ll sc[MAXN];
|
||||
|
||||
std::vector<int> adj[MAXN];
|
||||
int dist[MAXN][MAXN];
|
||||
|
||||
std::vector<int> pre[MAXN];
|
||||
|
||||
void bfs(int s) {
|
||||
std::queue<std::pair<int, int>> q;
|
||||
q.push({s, 0});
|
||||
dist[s][s] = 0;
|
||||
|
||||
while (!q.empty()) {
|
||||
auto [u, d] = q.front();
|
||||
q.pop();
|
||||
if (d > k) {
|
||||
continue;
|
||||
}
|
||||
for (int v : adj[u]) {
|
||||
if (dist[s][v] == -1) {
|
||||
dist[s][v] = d + 1;
|
||||
q.push({v, d + 1});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::ios_base::sync_with_stdio(false);
|
||||
std::cin.tie(nullptr);
|
||||
|
||||
std::cin >> n >> m >> k;
|
||||
|
||||
for (int i = 2; i <= n; ++i) {
|
||||
std::cin >> sc[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < m; ++i) {
|
||||
int u, v;
|
||||
std::cin >> u >> v;
|
||||
adj[u].push_back(v);
|
||||
adj[v].push_back(u);
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
for (int j = 1; j <= n; ++j) {
|
||||
dist[i][j] = -1;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
bfs(i);
|
||||
}
|
||||
|
||||
for (int i = 2; i <= n; ++i) {
|
||||
for (int j = 2; j <= n; ++j) {
|
||||
if (i == j)
|
||||
continue;
|
||||
if (dist[1][j] != -1 && dist[j][i] != -1) {
|
||||
pre[i].push_back(j);
|
||||
}
|
||||
}
|
||||
std::sort(pre[i].begin(), pre[i].end(), [&](int a, int b) { return sc[a] > sc[b]; });
|
||||
if (pre[i].size() > 3) {
|
||||
pre[i].resize(3);
|
||||
}
|
||||
}
|
||||
|
||||
ll ans = 0;
|
||||
for (int b = 2; b <= n; ++b) {
|
||||
for (int c = 2; c <= n; ++c) {
|
||||
if (b == c)
|
||||
continue;
|
||||
if (dist[b][c] != -1) {
|
||||
for (int a : pre[b]) {
|
||||
for (int d : pre[c]) {
|
||||
if (a != c && a != d && b != d) {
|
||||
ans = std::max(ans, sc[a] + sc[b] + sc[c] + sc[d]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << ans << std::endl;
|
||||
}
|
Loading…
Reference in New Issue
Block a user