QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#165956#5506. Hyperlooparseny_y#TL 1ms10708kbC++232.6kb2023-09-05 23:36:352023-09-05 23:36:35

Judging History

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

  • [2023-09-05 23:36:35]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:10708kb
  • [2023-09-05 23:36:35]
  • 提交

answer

//I wrote this code 4 u today
#include <bits/stdc++.h>

template<class t> using vc = std::vector<t>;

#define nd node*
#define pnd pair<nd, nd>

typedef long long ll;

template<const ll MOD>
struct mod_mul : std::multiplies<const ll> {
    ll operator()(const ll a, const ll b) {
        return (a * b) % MOD;
    }
};


template<typename T>
inline void sort(T &a) {
    std::sort(a.begin(), a.end());
}

template<typename T>
inline void unique(T &a) {
    a.resize(std::unique(a.begin(), a.end()) - a.begin());
}

template<typename T>
inline void reverse(T &a) {
    std::reverse(a.begin(), a.end());
}

const ll INF = 9023372036854775808ll;
const ll MOD = 1000000007ll;

using namespace std;

const int MAXN = 1e5 + 228;

vector<pair<int, int>> g[MAXN];
vector<pair<int,int>> ng[MAXN];

vector<int> usd(MAXN, false), nxt(MAXN, -1);

vector<int> dp[MAXN];

void dfs(int v = 1) {
    for (auto &[to, w] : ng[v]) {
        dfs(to);
    }
    for (auto &[to, w] : ng[v]) {
        vector<int> cur = dp[to];
        cur.push_back(w);
        sort(cur.begin(), cur.end(), greater<>());
        if (cur > dp[v]) {
            dp[v] = cur;
            nxt[v] = to;
        }
    }
}

int32_t main() {
    std::cin.tie(nullptr)->ios_base::sync_with_stdio(false);
    ll t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        for (int i = 1; i <= n; ++i) {
            g[i].clear(), ng[i].clear();
        }
        while (m--) {
            int x, y, w;
            cin >> x >> y >> w;
            g[x].emplace_back(y, w);
            g[y].emplace_back(x, w);
        }
        priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q;
        q.emplace(0, 1);
        vector<int> d(n + 1, INT_MAX);
        d[1] = 0;
        while (!q.empty()) {
            auto [curd, v] = q.top();
            q.pop();
            for (auto &[to, w] : g[v]) {
                if (d[v] + w < d[to]) {
                    d[to] = d[v] + w;
                    q.emplace(d[to], to);
                }
            }
        }
        for (int i = 1; i <= n; ++i) {
            nxt[i] = -1;
            for (auto &[to, w] : g[i]) {
                if (d[to] == d[i] + w) {
                    ng[i].push_back({to, w});
                }
            }
        }
        dfs();
        int nw = 1;
        cout << dp[1].size() + 1 << '\n';
        while (nw != -1) {
            if (nw == n) break;
            cout << nw << ' ';
            nw = nxt[nw];
        }
        cout << n;
        cout << '\n';
    }
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 10708kb

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: -100
Time Limit Exceeded

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: