比赛 2025.10.24 评测结果 AAAAAAEEEEEEEEEEEEEE
题目名称 坡伊踹 最终得分 30
用户昵称 wdsjl 运行时间 3.356 s
代码语言 C++ 内存使用 3.88 MiB
提交时间 2025-10-24 09:25:52
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;

const int N = 1005;    

int to[N<<1],w[N<<1],ne[N<<1],hd[N],cnt;

vector<int> a;  

void ade(int u, int v, int val) {
    to[cnt] = v;
    w[cnt] = val;
    ne[cnt] = hd[u];  
    hd[u] = cnt++;     
}

vector<int> g_p(int u, int v, int n) {
    vector<int> p(n + 1, -1); 
    vector<bool> vis(n + 1, false);
    queue<int> q;
    q.push(u);
    vis[u] = true;
    while (!q.empty()) {
        int cur = q.front();
        q.pop();
        if (cur == v) break;
        for (int i = hd[cur]; i != -1; i = ne[i]) {
            int vv = to[i];
            if (!vis[vv]) {
                vis[vv] = true;
                p[vv] = cur;
                q.push(vv);
            }
        }
    }
    vector<int> path;
    int cur = v;
    while (cur != -1) {
        path.push_back(cur);
        cur = p[cur];
    }
    reverse(path.begin(), path.end());
    return path;
}

int main() {
    freopen("poitry.in","r",stdin);
    freopen("poitry.out","w",stdout);
    int n, q;
    cin >> n >> q;
    a.resize(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    memset(hd, -1, sizeof(hd));
    cnt = 0;
    for (int i = 0; i < n - 1; ++i) {
        int u, v, w;
        cin >> u >> v >> w;
        ade(u, v, w);  
        ade(v, u, w);  
    }
    while (q--) {
        int u, v;
        cin >> u >> v;
        vector<int> path = g_p(u, v, n); 
        int m = path.size();
        vector<long long> dist(m, 0); 

        for (int i = 1; i < m; ++i) {
            int x = path[i-1], y = path[i];
            for (int j = hd[x]; j != -1; j = ne[j]) {
                if (to[j] == y) {
                    dist[i] = dist[i-1] + w[j];
                    break;
                }
            }
        }
        long long ans = LLONG_MAX;
        for (int i = 0; i < m; ++i) {
            int x = path[i];
            ans = min(ans, max((long long)a[x], dist[i]));
        }
        cout << ans << '\n';
    }
    return 0;
}