結果

問題 No.848 なかよし旅行
ユーザー んんんんんん
提出日時 2022-12-28 16:59:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 1,577 bytes
コンパイル時間 2,521 ms
コンパイル使用メモリ 209,756 KB
実行使用メモリ 10,648 KB
最終ジャッジ日時 2024-05-02 22:50:15
合計ジャッジ時間 5,241 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
10,648 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 33 ms
5,376 KB
testcase_12 AC 46 ms
5,760 KB
testcase_13 AC 59 ms
6,656 KB
testcase_14 AC 18 ms
5,376 KB
testcase_15 AC 55 ms
6,400 KB
testcase_16 AC 104 ms
8,320 KB
testcase_17 AC 73 ms
7,296 KB
testcase_18 AC 34 ms
5,376 KB
testcase_19 AC 28 ms
5,376 KB
testcase_20 AC 10 ms
5,376 KB
testcase_21 AC 85 ms
7,296 KB
testcase_22 AC 92 ms
7,168 KB
testcase_23 AC 22 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 109 ms
8,320 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using P = pair<long long, int>;
using Graph = vector<vector<P>>;

void dijkstra(const Graph &G, vector<long long> &dist, int s) {
    priority_queue<P, vector<P>, greater<P>> que;
    dist[s] = 0;
    que.emplace(dist[s], s);

    while (!que.empty()){
        auto p = que.top();
        que.pop();
        int v = p.second;
        if (dist[v] < p.first) continue;

        for (auto np : G[v]) {
            auto nv = np.second;
            auto dis = np.first;
            if (dist[nv] > dist[v] + dis) {
                dist[nv] = dist[v] + dis;
                que.emplace(dist[nv], nv);
            }
        }
    }
}

int main() {
    int N, M, P, Q;
    long long T;
    cin >> N >> M >> P >> Q >> T;
    P--; Q--;
    Graph G(N);
    for (int i = 0; i < M; i++) {
        int a, b;
        long long c;
        cin >> a >> b >> c;
        a--; b--;
        G[a].emplace_back(c, b);
        G[b].emplace_back(c, a);
    }

    vector<long long> dist(N, 1LL << 60), distP(N, 1LL << 60), distQ(N, 1LL << 60);
    dijkstra(G, dist, 0);
    dijkstra(G, distP, P);
    dijkstra(G, distQ, Q);

    if (dist[P] + dist[Q] + distP[Q] <= T) {
        cout << T << endl;
        return 0;
    }

    long long ans = -1;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (max(dist[i] + distP[i] + distP[j] + dist[j], dist[i] + distQ[i] + distQ[j] + dist[j]) > T) continue;
            ans = max(ans, T - max(distP[i] + distP[j], distQ[i] + distQ[j]));
        }
    }

    cout << ans << endl;
}
0