QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#46214#4479. SlippermiaomiaoziAC ✓1460ms154388kbC++172.1kb2022-08-26 17:45:502022-08-26 17:45:53

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-08-26 17:45:53]
  • 评测
  • 测评结果:AC
  • 用时:1460ms
  • 内存:154388kb
  • [2022-08-26 17:45:50]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
// https://space.bilibili.com/672346917

#ifndef LOCAL
#define LOG(...) 42
#endif

#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

typedef long long LL;
typedef pair <int, int> PII;

constexpr double EPS = 1e-8;
const double PI = acos(-1);

int multi_cases = 1;

void A_SOUL_AvA () {
    int n;
    cin >> n;

    vector <array<int, 2>> adj[n * 2 + 1];
    for (int i = 1; i < n; i++) {
        int u, v, w;
        cin >> u >> v >> w;

        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }

    int k, p, S, T;
    cin >> k >> p >> S >> T;

    vector <int> dep(n + 1);
    dep[1] = 1;
    function <void(int, int)> DFS = [&](int u, int fa) {
        for (auto &[v, w] : adj[u]) {
            if (v != fa) {
                dep[v] = dep[u] + 1;
                DFS(v, u);
            }
        }
    };
    DFS(1, -1);

    for (int i = 1; i <= n; i++) {
        int x = dep[i] + n;
        adj[i].pb({x, 0});
        if (x - k >= n + 1) adj[x - k].pb({i, p});
        if (x + k <= n * 2) adj[x + k].pb({i, p});
    }

    const LL inf = 1e18;
    auto dij = [&]() {
        vector <LL> dis(n * 2 + 5, inf);
        vector <int> st(n * 2 + 5);
        dis[S] = 0;
        priority_queue <pair<LL, int>, vector<pair<LL, int>>, greater<>> q;
        q.push({0, S});

        while (q.size()) {
            auto [d, u] = q.top();
            q.pop();
            if (st[u]) continue;
            st[u] = 1;

            for (auto &[v, w] : adj[u]) {
                if (dis[v] > d + w) {
                    dis[v] = d + w;
                    q.push({dis[v], v});
                }
            }
        }
        return dis[T];
    };

    cout << dij() << endl;
}

int main () {
    cin.tie(nullptr)->sync_with_stdio(false);
    cout << fixed << setprecision(12);

    int T = 1;
    for (multi_cases && cin >> T; T; T--) {
        A_SOUL_AvA ();
    }

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1460ms
memory: 154388kb

input:

5
121753
103252 40559 325002
32674 51809 946614
18343 12099 625962
27677 48601 114048
11146 12478 906161
121147 77390 208299
39512 95642 154696
90603 43508 378490
4829 7818 191754
73699 31412 536840
106916 89894 374802
113739 90049 411062
113123 73246 740213
38047 120942 903325
51907 41500 822541
90...

output:

114128108
55207815
76620494
17377950755
67601

result:

ok 5 lines