社区讨论

求调整!

P8817[CSP-S 2022] 假期计划参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@mhjripzo
此快照首次捕获于
2025/11/04 07:19
4 个月前
此快照最后确认于
2025/11/04 07:19
4 个月前
查看原帖
CPP
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 10005;
vector<int> G[N];
int scores[N];
int d[N][N];
int b[N][3];
int n, m, k;
void bfs(int start) {
    for (int i = 1; i <= n; i ++) {
        d[start][i] = -1;
    }
    d[start][start] = 0;
    queue<int> q;
    q.push(start);
    while (q.size()) {
        int f = q.front();
        q.pop();
        if (d[start][f] == k) {
            continue;
        }
        for (int i = 0; i < G[f].size(); i ++) {
            int v = G[f][i];
            if (d[start][v] == -1) {
                d[start][v] = d[start][f] + 1;
                q.push(v);
            }
        }
    }
    return;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n >> m >> k;
    for (int i = 2; i <= n; i ++) {
        cin >> scores[i];
    }
    while (m --) {
        int x, y;
        cin >> x >> y;
        G[x].push_back(y);
        G[y].push_back(x);
    }
    k ++;

    //bfs搜索每个点能够到达的邻居
    for (int i = 1; i <= n; i ++) {
        bfs(i);
    }

    for (int i = 2; i <= n; i ++) {
        int first = b[i][0], second = b[i][1], third = b[i][2];
        for (int j = 2; j <= n; j ++) {
            if (i == j || d[1][j] == -1 || d[i][j] == -1) {
                continue;
            }
            if (scores[j] > scores[first]) {
                third = second;
                second = first;
                first = j;
            }
            else if (scores[j] > scores[second]) {
                third = second;
                second = first;
            }
            else if (scores[j] > scores[third]) {
                third = j;
            }
            b[i][0] = first;
            b[i][1] = second;
            b[i][2] = third;
        }
    }
    LL ans = 0;
    for (int i = 2; i <= n; i ++) {
        for (int j = i + 1; j <= n; j ++) {
            if (d[i][j] == -1) {
                continue;
            }
            for (int f = 0; f < 3; f ++) {
                for (int s = 0; s < 3; s ++) {
                    int x = b[i][f], y = b[j][s];
                    if (x == 0 || y == 0 || x == y || x == i || x == j || y == i || y == j) {
                        continue;
                    }
                    int t = scores[i] + scores[j] + scores[x] + scores[y];
                    ans = (ans > t ? ans : t);
                }
            }
        }
    }
    cout << ans;
    return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...