結果

問題 No.2805 Go to School
ユーザー InTheBloomInTheBloom
提出日時 2024-07-12 22:28:58
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 341 ms / 2,000 ms
コード長 2,232 bytes
コンパイル時間 1,719 ms
コンパイル使用メモリ 103,300 KB
実行使用メモリ 18,912 KB
最終ジャッジ日時 2024-07-16 01:41:02
合計ジャッジ時間 7,975 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 167 ms
12,928 KB
testcase_05 AC 202 ms
9,340 KB
testcase_06 AC 106 ms
6,940 KB
testcase_07 AC 102 ms
6,940 KB
testcase_08 AC 180 ms
9,344 KB
testcase_09 AC 105 ms
6,940 KB
testcase_10 AC 112 ms
6,944 KB
testcase_11 AC 341 ms
16,896 KB
testcase_12 AC 203 ms
10,948 KB
testcase_13 AC 287 ms
13,556 KB
testcase_14 AC 43 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 147 ms
8,296 KB
testcase_19 AC 92 ms
7,264 KB
testcase_20 AC 228 ms
10,864 KB
testcase_21 AC 282 ms
17,116 KB
testcase_22 AC 122 ms
9,472 KB
testcase_23 AC 204 ms
10,856 KB
testcase_24 AC 200 ms
10,568 KB
testcase_25 AC 59 ms
7,608 KB
testcase_26 AC 218 ms
18,912 KB
testcase_27 AC 151 ms
15,212 KB
testcase_28 AC 12 ms
6,940 KB
testcase_29 AC 22 ms
7,808 KB
testcase_30 AC 81 ms
11,664 KB
testcase_31 AC 113 ms
8,652 KB
testcase_32 AC 227 ms
13,676 KB
testcase_33 AC 236 ms
14,944 KB
testcase_34 AC 2 ms
6,944 KB
testcase_35 AC 2 ms
6,944 KB
testcase_36 AC 106 ms
7,936 KB
testcase_37 AC 109 ms
9,216 KB
testcase_38 AC 202 ms
13,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

using namespace std;
using ll = long long;

constexpr int iINF = 1'000'000'000;
constexpr ll llINF = 1'000'000'000'000'000'000;

int main () {
    int N, M, L, S, E; cin >> N >> M >> L >> S >> E;
    vector<vector<pair<int, int>>> graph(N);
    for (int i = 0; i < M; i++) {
        int a, b, t; cin >> a >> b >> t;
        a--, b--;
        graph[a].push_back(make_pair(b, t));
        graph[b].push_back(make_pair(a, t));
    }
    vector<int> T(L);
    for (int i = 0; i < L; i++) cin >> T[i];
    for (int i = 0; i < L; i++) T[i]--;

    // トイレを中継地点として両端から探索すればよさそう。
    auto cmp = [&] (pair<int, ll>& a, pair<int, ll>& b) {
        return b.second < a.second;
    };
    priority_queue<pair<int, ll>, vector<pair<int, ll>>, decltype(cmp)> pq(cmp);

    vector<ll> home_to(N, llINF);

    pq.push(make_pair(0, 0LL));
    while (!pq.empty()) {
        auto pos = pq.top(); pq.pop();
        int u = pos.first; ll c = pos.second;
        if (home_to[u] < c) continue;
        home_to[u] = c;

        for (auto to : graph[u]) {
            int v = to.first;
            ll new_cost = c + to.second;

            if (home_to[v] <= new_cost) continue;
            home_to[v] = new_cost;
            pq.push(make_pair(v, new_cost));
        }
    }

    vector<ll> school_to(N, llINF);

    pq.push(make_pair(N - 1, 0LL));
    while (!pq.empty()) {
        auto pos = pq.top(); pq.pop();
        int u = pos.first; ll c = pos.second;
        if (school_to[u] < c) continue;
        school_to[u] = c;

        for (auto to : graph[u]) {
            int v = to.first;
            ll new_cost = c + to.second;

            if (school_to[v] <= new_cost) continue;
            school_to[v] = new_cost;
            pq.push(make_pair(v, new_cost));
        }
    }

    ll ans = llINF;
    for (auto i : T) {
        if (home_to[i] == llINF || school_to[i] == llINF) continue;
        if (S + E <= home_to[i]) continue; // 間に合わなかった...

        ans = min(ans, max(1LL * S, home_to[i]) + 1 + school_to[i]);
    }

    if (ans == llINF) {
        cout << -1 << "\n";
    }
    else {
        cout << ans << "\n";
    }
}
0