CSP-S T1 题解

· · 个人记录

考场上我甚至想到了记录前三大然后分讨,然后以为分类讨论要写一堆如果,然后挂了,打了个暴力。。。

正解

考虑整个路径是由 1,A,B,C,D,1 个组成,由于 1,A,B 和 C,D,1 是对称的,所以我们可以考虑枚举 B,C 两个点,然后直接在 O(1) 内求出 A,D。

做法就是我们记录对于每个点,所有点中与这个点本身距离小于等于 k,且与 1 的距离也小于等于 k 的点中权值前 3 大的值。

这样当我们选择好了 B,C 两点之后,只需要枚举 B,C 的前三大中不重复的 2 个点权和最大的点,然后与 B,C 两点的点权相加,就得出了解。

复杂度

预处理距离是 O(nm) 的。

求点权前三大和最后求和都是 O(n^2) 的。

所以总复杂度是 O(nm+n^2) 的。

AC Code

#include <bits/stdc++.h>

using namespace std;

const int N = 2510, M = 1e4 + 10;
int hd[N], nxt[M * 2], ver[M * 2], idx;
typedef long long LL;

int dis[N][N], n, m, k, d[N][3];
LL s[N], res;

inline void add (int x, int y) {
    ver[++idx] = y;
    nxt[idx] = hd[x];
    hd[x] = idx;
}

void bfs (int st) {
    queue <int> q;
    dis[st][st] = -1;
    q.push(st);
    while (q.size()) {
        int t = q.front(); q.pop();
        for (int i = hd[t]; i ;i = nxt[i]) {
            int y = ver[i];
            if (dis[st][t] + 1 < dis[st][y]) {
                dis[st][y] = dis[st][t] + 1;
                q.push(y);
            }
        }
    }
}

int main () {
//  freopen("holiday.in", "r", stdin);
//  freopen("holiday.out", "w", stdout);
    memset(dis, 0x3f, sizeof(dis));
    s[0] = -0x3f3f3f3f3f3f3f3f;
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 2;i <= n;i++) scanf("%lld", &s[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);
    for (int i = 2;i <= n;i++) {
        for (int j = 2;j <= n;j++) {
            if (j == i) continue;
            if (dis[j][1] <= k && dis[i][j] <= k) {
                if (s[j] >= s[d[i][0]]) d[i][2] = d[i][1], d[i][1] = d[i][0], d[i][0] = j;
                else if (s[j] >= s[d[i][1]]) d[i][2] = d[i][1], d[i][1] = j;
                else if (s[j] > d[i][2]) d[i][2] = j;
            }
        }
    }
    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 <= 2;p++) {
                for (int q = 0;q <= 2;q++) {
                    if (d[i][p] != j && d[i][p] != d[j][q] && d[j][q] != i) {
                        if (s[d[i][p]] + s[d[j][q]] + s[i] + s[j] > res) {
                            res = s[d[i][p]] + s[d[j][q]] + s[i] + s[j];
                        }
                    }
                }
            }
        }
    }
    printf("%lld\n", res);
    return 0;
} 

再来一发我的考场 Code(75pts)

#include <bits/stdc++.h>

using namespace std;

const int N = 2510, M = 1e4 + 10;
int hd[N], nxt[M * 2], ver[M * 2], idx;
typedef long long LL;

int dis[N][N], n, m, k;
LL s[N], res;

inline void add (int x, int y) {
    ver[++idx] = y;
    nxt[idx] = hd[x];
    hd[x] = idx;
}

void bfs (int st) {
    queue <int> q;
    dis[st][st] = -1;
    q.push(st);
    while (q.size()) {
        int t = q.front(); q.pop();
        if (dis[st][t] >= k) return;
        for (int i = hd[t]; i ;i = nxt[i]) {
            int y = ver[i];
            if (dis[st][t] + 1 < dis[st][y]) {
                dis[st][y] = dis[st][t] + 1;
                q.push(y);
            }
        }
    }
}

int main () {
//  freopen("holiday.in", "r", stdin);
//  freopen("holiday.out", "w", stdout);
    memset(dis, 0x3f, sizeof(dis));
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 2;i <= n;i++) scanf("%lld", &s[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);
    for (int j1 = 2;j1 <= n;j1++) {
        if (dis[1][j1] > k) continue;
        for (int j2 = 2;j2 <= n;j2++) {
            if (j2 == j1 || dis[j1][j2] > k) continue;
            for (int j3 = 2;j3 <= n;j3++) {
                if (j3 == j2 || j3 == j1 || dis[j3][j2] > k) continue;
                for (int j4 = 2;j4 <= n;j4++) {
                    if (j4 == j1 || j4 == j2 || j4 == j3 || dis[j4][j3] > k || dis[j4][1] > k) continue;
                    res = max(res, s[j1] + s[j2] + s[j3] + s[j4]);
                }
            }
        }
    }
    printf("%lld\n", res);
    return 0;
}