75分(超时)求调
查看原帖
75分(超时)求调
982681
D0000楼主2023/7/18 11:00
#include <iostream>
using namespace std;

int n, m, k, ans;
struct node {
    int n, cnt, aa[2500], u[5], ju;
} a[2501];

void dfs(int step, int t, int nowstep, int sc) {
    if (nowstep == k && t != 1 && a[t].u[0] == 0) {
        if (step == 4 && a[t].ju <= k + 1) {
            ans = max(ans, sc + a[t].n);
            return;
        }
        if (step == 4 && a[t].ju > k + 1)
            return;

        a[t].u[0] = 1;
        dfs(step + 1, t, -1, sc + a[t].n);
        a[t].u[0] = 0;

        for (int i = 1; i <= n; i++)
            a[i].u[step + 1] = 0;

        return;
    }

    if (nowstep == k || (a[t].u[0] == 1 && nowstep == k))
        return;

    for (int i = 0; i < a[t].cnt; i++) {
        if (a[a[t].aa[i]].u[step] == 0) {
            a[a[t].aa[i]].u[step] = 1;
            dfs(step, a[t].aa[i], nowstep + 1, sc);
        }
    }

    if (t != 1 && a[t].u[0] == 0 && step <= 3) {
        a[t].u[0] = 1;
        dfs(step + 1, t, -1, sc + a[t].n);
        a[t].u[0] = 0;

        for (int i = 1; i <= n; i++)
            a[i].u[step + 1] = 0;
    }

    if (step == 4 && a[t].ju <= k + 1 && t != 1 && a[t].u[0] == 0)
        ans = max(ans, sc + a[t].n);
}

int main() {
    cin >> n >> m >> k;

    for (int i = 2; i <= n; i++)
        cin >> a[i].n;

    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;

        a[x].aa[a[x].cnt] = y;
        a[y].aa[a[y].cnt] = x;

        a[x].cnt++;
        a[y].cnt++;
    }

    int que[2500], head = 0, tail = 0;
    bool b[2501] = {0};

    que[head] = 1;
    a[1].ju = 0;
    b[1] = 1;

    while (head <= tail) {
        int x = que[head];

        for (int i = 0; i < a[x].cnt; i++) {
            if (!b[a[x].aa[i]]) {
                tail++;
                que[tail] = a[x].aa[i];
                b[que[tail]] = 1;

                a[que[tail]].ju = a[que[head]].ju + 1;
            }
        }

        head++;
    }

    a[1].u[1] = 1;
    dfs(1, 1, -1, 0);

    cout << ans;

    return 0;
}
2023/7/18 11:00
加载中...