QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#155042#5506. HyperloopThegeekKnight16WA 261ms3744kbC++173.0kb2023-09-01 07:29:092023-09-01 07:29:11

Judging History

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

  • [2023-09-01 07:29:11]
  • 评测
  • 测评结果:WA
  • 用时:261ms
  • 内存:3744kb
  • [2023-09-01 07:29:09]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
vector<int> pai, sub, Dist[2], pai2;
vector<vector<pair<int, int>>> grafo;
vector<vector<int>> grafo2;

int find(int v) {return pai[v] = (v == pai[v] ? v : find(pai[v]));}
void une(int x, int y)
{
    x = find(x); y = find(y);
    if (x == y) return;
    if (sub[x] < sub[y]) swap(x, y);
    pai[y] = x;
    sub[x] += sub[y];
}

void dijkstra(int S, int type)
{
    // cerr << "ERRO " << type << '\n';
    set<pair<int, int>> fora;
    fora.emplace(0, S); Dist[type][S] = 0;

    while (!fora.empty())
    {
        auto [d, v] = *fora.begin(); fora.erase(fora.begin());
        // cerr << '\t' << v << " " << d << '\n';

        for (auto [viz, p] : grafo[v])
        {
            if (Dist[type][viz] > d + p)
            {
                fora.erase(make_pair(Dist[type][viz], viz));
                fora.emplace(Dist[type][viz] = d + p, viz);
            }
        }
    }
}

void dfs(int v, int p)
{
    // cerr << "ERRO " << 2 << '\n';
    pai2[v] = p;

    for (auto viz : grafo2[v])
    {
        if (viz == p) continue;
        dfs(viz, v);
    }
}

void solve()
{
    int N, M;
    cin >> N >> M;
    // cerr << N << " " << M << '\n';
    // cerr << "ERRO " << 3 << '\n';
    //Limpar as coisas
    {
        pai.resize(N+1); iota(pai.begin(), pai.end(), 0);
        sub.resize(N+1); fill(sub.begin(), sub.end(), 1);
        pai2.resize(N+1);
        grafo.clear(); grafo.resize(N+1);
        grafo2.clear(); grafo2.resize(N+1);
        Dist[0].resize(N+1); fill(Dist[0].begin(), Dist[0].end(), INF);
        Dist[1].resize(N+1); fill(Dist[1].begin(), Dist[1].end(), INF);
    }
    vector<tuple<int, int, int>> arestas(M);
    for (auto &[P, X, Y] : arestas)
    {
        cin >> X >> Y >> P;
        grafo[X].emplace_back(Y, P);
        grafo[Y].emplace_back(X, P);
    }
    // cerr << "ERRO " << 4 << '\n';
    dijkstra(1, 0); dijkstra(N, 1);

    // cerr << "ERRO " << 6 << '\n';
    for (int i = 0; i < arestas.size(); i++)
    {
        auto [P, X, Y] = arestas[i];
        if (Dist[0][X] + P + Dist[1][Y] == Dist[0][N]) continue;
        if (Dist[0][Y] + P + Dist[1][X] == Dist[0][N]) continue;
        swap(arestas.back(), arestas[i]);
        arestas.pop_back(); --i;
    }

    sort(arestas.rbegin(), arestas.rend());

    // cerr << "ERRO " << 5 << '\n';
    for (auto [P, X, Y] : arestas)
    {
        if (find(X) == find(Y)) continue;
        une(X, Y);
        grafo2[X].push_back(Y);
        grafo2[Y].push_back(X);
    }
    dfs(1, 1);

    // cerr << "ERRO " << 7 << '\n';
    int x = N; vector<int> resp(1, N);
    while (pai2[x] != x)
    {
        x = pai2[x];
        resp.push_back(x);
    }
    reverse(resp.begin(), resp.end());
    cout << resp.size() << '\n';
    for (auto r : resp) cout << r << " ";
    cout << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int T;
    cin >> T;
    while (T--) solve();
}

详细

Test #1:

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

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 3 4 
5
1 2 5 3 6 

result:

ok correct (2 test cases)

Test #2:

score: -100
Wrong Answer
time: 261ms
memory: 3744kb

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 3)