QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#165964#5506. Hyperlooparseny_y#ML 1ms10924kbC++233.1kb2023-09-05 23:44:522023-09-05 23:44:53

Judging History

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

  • [2023-09-05 23:44:53]
  • 评测
  • 测评结果:ML
  • 用时:1ms
  • 内存:10924kb
  • [2023-09-05 23:44:52]
  • 提交

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];
        vector<int> ncur;
        if (!cur.empty() && w > cur[0]) {
            cur.insert(cur.begin(), w);
        } else if (!cur.empty()) {
            for (int i = 0; i < cur.size(); ++i) {
                ncur.push_back(cur[i]);
                if (i == cur.size() - 1 || cur[i] >= w && w >= cur[i + 1]) {
                    ncur.push_back(w);
                }
            }
            cur = ncur;
        } else {
            cur = {w};
        }
        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;
        vector<int> ans;
        for (int i = 0; i < dp[1].size(); ++i) {
            if (nw != -1 && nw != n) {
                ans.push_back(nw);
            } else break;
            nw = nxt[nw];
        }
        ans.push_back(n);
        cout << ans.size() << '\n';
        for (auto &el : ans) cout << el << ' ';
        cout << '\n';
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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
Memory 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:


result: