题解:P16901 [CCO 2026] Waterloo Tag

· · 题解

设学生的出发点为 k,从 1 到 x 的距离为 f_x,从 k 出发到 x 的距离为 g_k。

枚举会在哪一条边上停下(包括端点),设在 x 处进入 (x, v, w) 这条边,分为两种情况:

两个时间取小值,答案是每条边的时间的最大值。

void dij(ll s, ll *dis) {
    for(ll i = 1; i <= n; i++) dis[i] = inf, vis[i] = 0;
    priority_queue <pair <ll, ll> > q;
    q.push({0, s}), dis[s] = 0;
    while(q.size()) {
        ll x = q.top().second; q.pop();
        if(vis[x]) continue;
        vis[x] = 1;
        for(auto it : e[x]) {
            ll v = it.first, w = it.second;
            if(*dis && (Frac){dis[x] + w, vf} >= (Frac){g[v], vg}) continue;
            if(dis[v] > dis[x] + w) 
                dis[v] = dis[x] + w, q.push({-dis[v], v});
        }
    }
}
signed main() {
    n = rd(), m = rd(), vf = rd(), vg = rd();
    *f = 1;
    for(ll i = 1; i <= m; i++) {
        ll u = rd(), v = rd(), w = rd();
        e[u].push_back({v, w}), e[v].push_back({u, w});
    }
    for(ll k = 2; k <= n; k++) {
        dij(k, g), dij(1, f);
        Frac ans = {0, 1};
        for(ll x = 1; x <= n; x++) if(f[x] < inf) {
            for(auto it : e[x]) {
                ll v = it.first, w = it.second;
                Frac res = {g[x] + g[v] + w, vg + vg};
                if(vf < vg) res = min(res, {g[x] - f[x], vg - vf});
                res.init(), ans = max(ans, res);
            }
        }
        cout << ans.p << '/' << ans.q << '\n';
    }
    return 0;
}