QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#91077#5506. Hyperloopskittles1412ML 293ms4160kbC++173.5kb2023-03-27 01:14:282023-03-27 01:14:29

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-27 01:14:29]
  • 评测
  • 测评结果:ML
  • 用时:293ms
  • 内存:4160kb
  • [2023-03-27 01:14:28]
  • 提交

answer

#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
    cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
    cerr << t << " | ";
    dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
    cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
         << ": ";                                          \
    dbgh(__VA_ARGS__)
#else
#define cerr   \
    if (false) \
    cerr
#define dbg(...)
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))

template <typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;

vector<long> dijkstra(int src, const vector<vector<pair<int, int>>>& graph) {
    int n = sz(graph);
    vector<long> dist(n, 1e18);
    min_pq<pair<long, int>> pq;
    auto push = [&](int u, long d) -> void {
        if (d < dist[u]) {
            dist[u] = d;
            pq.emplace(d, u);
        }
    };
    push(src, 0);
    while (sz(pq)) {
        auto [d, u] = pq.top();
        pq.pop();
        for (auto& [v, w] : graph[u]) {
            push(v, d + w);
        }
    }
    return dist;
}

vector<vector<pair<int, int>>> filter_sp(int n,
                                         const vector<long>& dist,
                                         vector<vector<pair<int, int>>> graph) {
    for (int i = 0; i < n; i++) {
        auto& a = graph[i];
        a.erase(remove_if(begin(a), end(a),
                          [&](const auto& e) -> bool {
                              auto& [v, w] = e;
                              return dist[i] + w != dist[v];
                          }),
                a.end());
    }
    return graph;
}

struct S {
    static constexpr int bsize = 350;

    array<int, bsize> small {};
    vector<int> large;

    void insert(int x) {
        if (x < bsize) {
            small[bsize - x - 1]++;
        } else {
            large.insert(upper_bound(begin(large), end(large), x, greater<>()), x);
        }
    }

    bool operator<(const S& s) const {
        if (large == s.large) {
            return small < s.small;
        }
        return large < s.large;
    }
};

void solve() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> graph(n);
    for (int i = 0; i < m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        u--;
        v--;
        graph[u].emplace_back(v, w);
        graph[v].emplace_back(u, w);
    }
    auto dist = dijkstra(0, graph);
    graph = filter_sp(n, dist, graph);
    int ord[n];
    iota(ord, ord + n, 0);
    sort(ord, ord + n, [&](int a, int b) -> bool {
        return dist[a] < dist[b];
    });
    S dp[n];
    int from[n];
    for (auto& a : ord) {
        for (auto& [v, w] : graph[a]) {
            auto tmp = dp[a];
            tmp.insert(w);
            if (dp[v] < tmp) {
                dp[v] = tmp;
                from[v] = a;
            }
        }
    }
    vector<int> ans;
    int u = n - 1;
    while (true) {
        ans.push_back(u);
        if (!u) {
            break;
        }
        u = from[u];
    }
    reverse(begin(ans), end(ans));
    cout << sz(ans) << endl;
    for (int i = 0; i < sz(ans); i++) {
        cout << ans[i] + 1 << " \n"[i == sz(ans) - 1];
    }
}

int main() {
    cin.tie(nullptr);
    cin.exceptions(ios::failbit);
    ios_base::sync_with_stdio(false);
    int tcs;
    cin >> tcs;
    while (tcs--) {
        solve();
    }
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3416kb

input:

2
4 6
1 2 1
1 3 2
2 3 1
2 4 2
3 4 1
1 4 4
6 11
1 2 9
2 3 12
3 4 3
4 5 5
5 6 10
6 1 22
2 4 9
3 6 1
4 6 5
2 5 2
3 5 8

output:

3
1 2 4
5
1 2 5 3 6

result:

ok correct (2 test cases)

Test #2:

score: 0
Accepted
time: 293ms
memory: 4160kb

input:

600
320 1547
204 81 13768
232 97 9939
97 249 3719
201 109 14322
183 132 40881
142 143 1
275 186 24548
18 236 7907
30 317 11845
131 130 1
311 300 11704
141 92 41925
174 191 32128
119 120 1
184 183 1
310 309 1
283 270 25477
233 141 36076
212 92 13770
307 110 40656
218 137 14033
180 85 41892
200 199 44...

output:

184
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10...

result:

ok correct (600 test cases)

Test #3:

score: -100
Memory Limit Exceeded

input:

4
100000 220000
48940 43355 42347
77914 77913 1
45236 82683 42904
22563 16038 34866
81537 81538 43088
49803 51485 25497
63071 25523 14336
44102 39850 43782
13607 92386 16724
98711 73651 46840
17775 16801 28765
5757 98829 13508
85095 48444 1
9198 43003 32678
14461 14462 1
20245 48742 18138
89120 8911...

output:


result: