QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#268042#5506. Hyperloopkilo_tobo_tarjenWA 797ms12608kbC++204.5kb2023-11-27 23:44:502023-11-27 23:44:50

Judging History

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

  • [2023-11-27 23:44:50]
  • 评测
  • 测评结果:WA
  • 用时:797ms
  • 内存:12608kb
  • [2023-11-27 23:44:50]
  • 提交

answer

#include <bits/stdc++.h>
using i64 = long long;
using namespace std;
const int N = 1e5 + 5;
// const int B = 500;
// const int M = 50005;
// const int base = 17131;
const int mod = 998244353;
// const int mod = 1e9 + 7;
// const double pi = acos(-1);

int n, m, vis[N];
i64 dis1[N], dis2[N];
vector<pair<int, int>> adj[N], G[N], F[N];
void solve()
{
    cin >> n >> m;
    for (int i = 1, u, v, w; i <= m; i++)
    {
        cin >> u >> v >> w;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
    for (int i = 1; i <= n; i++)
    {
        vis[i] = 0;
        dis1[i] = dis2[i] = 1e18;
    }
    priority_queue<pair<i64, int>> que;
    que.push({0, 1});
    dis1[1] = 0;
    while (!que.empty())
    {
        int cur = que.top().second;
        que.pop();
        if (vis[cur])
            continue;
        vis[cur] = 1;
        for (auto &[i, w] : adj[cur])
        {
            if (dis1[i] > dis1[cur] + w)
            {
                dis1[i] = dis1[cur] + w;
                que.push({-dis1[i], i});
            }
        }
    }
    for (int i = 1; i <= n; i++)
        vis[i] = 0;
    que.push({0, n});
    dis2[n] = 0;
    while (!que.empty())
    {
        int cur = que.top().second;
        que.pop();
        if (vis[cur])
            continue;
        vis[cur] = 1;
        for (auto &[i, w] : adj[cur])
        {
            if (dis2[i] > dis2[cur] + w)
            {
                dis2[i] = dis2[cur] + w;
                que.push({-dis2[i], i});
            }
        }
    }
    set<int> tmp;
    for (int i = 1; i <= n; i++)
        for (auto &[j, w] : adj[i])
            if (dis1[i] + dis2[j] + w == dis1[n])
                G[i].push_back({j, w}), F[j].push_back({i, w}), tmp.insert(w);
    vector<int> val = {tmp.begin(), tmp.end()};
    reverse(val.begin(), val.end());
    for (int x : val)
    {
        vector<int> from(n + 1), to(n + 1);
        queue<int> que;
        vector<int> deg1(n + 1);
        for (int i = 1; i <= n; i++)
            for (auto [j, w] : G[i])
                deg1[j]++;
        for (int i = 1; i <= n; i++)
            if (!deg1[i])
                que.push(i);
        while (!que.empty())
        {
            int cur = que.front();
            que.pop();
            for (auto [i, w] : G[cur])
            {
                int val = from[cur];
                if (w == x)
                    val++;
                from[i] = max(from[i], val);
                if (!--deg1[i])
                    que.push(i);
            }
        }
        int mx1 = *max_element(from.begin(), from.end());
        vector<int> deg2(n + 1);
        for (int i = 1; i <= n; i++)
            for (auto [j, w] : F[i])
                deg2[j]++;
        for (int i = 1; i <= n; i++)
            if (!deg2[i])
                que.push(i);
        while (!que.empty())
        {
            int cur = que.front();
            que.pop();
            for (auto [i, w] : F[cur])
            {
                int val = to[cur];
                if (w == x)
                    val++;
                to[i] = max(to[i], val);
                if (!--deg2[i])
                    que.push(i);
            }
        }
        int mx2 = *max_element(to.begin(), to.end());
        assert(mx1 == mx2);
        set<pair<int, int>> vis;
        for (int i = 1; i <= n; i++)
            for (auto [j, w] : G[i])
                if (w < x && from[i] != mx1 && to[j] != mx2)
                    vis.insert({i, j}), vis.insert({j, i});
        for (int i = 1; i <= n; i++)
        {
            vector<pair<int, int>> tmp;
            for (auto [j, w] : G[i])
                if (!vis.count({i, j}))
                    tmp.push_back({j, w});
            G[i] = tmp;
            tmp.clear();
            for (auto [j, w] : F[i])
                if (!vis.count({i, j}))
                    tmp.push_back({j, w});
            F[i] = tmp;
            tmp.clear();
        }
    }
    vector<int> ans{1};
    int cur = 1;
    while (cur != n)
    {
        cur = G[cur][0].first;
        ans.push_back(cur);
    }
    cout << ans.size() << '\n';
    for (int i : ans)
        cout << i << ' ';
    cout << '\n';
    for (int i = 1; i <= n; i++)
    {
        adj[i].clear();
        G[i].clear();
        F[i].clear();
    }
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    cin >> t;
    cout << fixed << setprecision(12);
    while (t--)
        solve();
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 10836kb

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
Wrong Answer
time: 797ms
memory: 12608kb

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:

wrong answer Contestant's path is not optimal lexicographically (test case 10)