CE求助
查看原帖
CE求助
781574
skyworld666楼主2023/7/6 22:00

如题,拿chatGPT写得程序跑都跑不动。。。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2505;
const ll inf = 1e18;
int n, m, k, u, v;
ll w[N], g[N][N], ans;
struct node {
    int i;
    ll w;
} p[N][N];
bool cmp(node a, node b) {
    return a.w > b.w;
}
int head[N], cnt1;
struct edge {
    int to, next;
} e[N * 2];
bool vis[N];
queue<int> qq;
void bfs(int s) {
    memset(vis, 0, sizeof(vis));
    g[s][s] = 0;
    qq.push(s);
    while (!qq.empty()) {
        int u = qq.front();
        qq.pop();
        vis[u] = 1;
        for (int i = head[u]; i; i = e[i].next) {
            int v = e[i].to;
            if (!vis[v] && g[s][v] > g[s][u] + 1) {
                g[s][v] = g[s][u] + 1;
                qq.push(v);
            }
        }
    }
}
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> k;
    memset(g, 63, sizeof(g));
    for (int i = 2; i <= n; i++)
        cin >> w[i];
    for (int i = 1; i <= m; i++) {
        cin >> u >> v;
        cnt1++;
        e[cnt1].to = v;
        e[cnt1].next = head[u];
        head[u] = cnt1;
        cnt1++;
        e[cnt1].to = u;
        e[cnt1].next = head[v];
        head[v] = cnt1;
    }
    for (int i = 1; i <= n; i++) {
        bfs(i);
    }
    for (int j = 2; j <= n; j++) {
        for (int i = 2; i <= n; i++) {
            if (i != j && g[1][i] <= k + 1 && g[i][j] <= k + 1) {
                p[j][++p[j][0].i].i = i;
                p[j][p[j][0].i].w = w[j] + w[i];
            }
        }
        sort(p[j] + 1, p[j] + 1 + p[j][0].i, cmp);
    }
    for (int c = 2; c <= n; c++) {
        for (int b = 2; b <= n; b++) {
            if (c == b || g[c][b] > k + 1)
                continue;
            for (int ci = 1; ci <= min(3LL, p[c][0].i); ci++) {
                int d = p[c][ci].i;
                if (d == b)
                    continue;
                for (int bi = 1; bi <= min(3LL, p[b][0].i); bi++) {
                    int a = p[b][bi].i;
                    if (a == c || a == d)
                        continue;
                    ans = max(ans, p[c][ci].w + p[b][bi].w);
                }
            }
        }
    }
    cout << ans;
    return 0;
}
2023/7/6 22:00
加载中...