結果

問題 No.848 なかよし旅行
ユーザー rpy3cpprpy3cpp
提出日時 2019-09-09 17:39:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,692 bytes
コンパイル時間 2,342 ms
コンパイル使用メモリ 177,744 KB
実行使用メモリ 6,772 KB
最終ジャッジ日時 2023-09-10 21:48:37
合計ジャッジ時間 5,892 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
6,772 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 1 ms
4,380 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
4,384 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 20 ms
4,880 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 2 ms
4,380 KB
testcase_25 WA -
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr int INF = 1'000'000'009;

struct Edge{
    int to;
    int cost;
    Edge(int to, int cost):to(to), cost(cost) {}
};

vector<int> dijkstra(int start, const vector<vector<Edge>> &Es, const int distlimit){
    int N = Es.size();
    vector<int> dist(N, INF);
    dist[start] = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    pq.push(make_pair(0, start));
    while (not pq.empty()){
        int d, v;
        tie(d, v) = pq.top();
        pq.pop();
        if (d > dist[v]) continue;
        dist[v] = d;
        for (auto &uc : Es[v]){
            int u = uc.to;
            int new_d = d + uc.cost;
            if (new_d < dist[u] and new_d <= distlimit){
                dist[u] = new_d;
                pq.push(make_pair(new_d, u));
            }
        }
    }
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M, P, Q, T;
    cin >> N >> M >> P >> Q >> T;
    --P;
    --Q;
    vector<vector<Edge>> Es(N, vector<Edge>());
    for (int m = 0; m < M; ++m){
        int a, b, c;
        cin >> a >> b >> c;
        Es[a - 1].emplace_back(Edge(b - 1, c));
        Es[b - 1].emplace_back(Edge(a - 1, c));
    }
    auto dist0 = dijkstra(0, Es, T);
    auto distP = dijkstra(P, Es, T);
    auto distQ = dijkstra(Q, Es, T);
    if (dist0[P] + distP[Q] + distQ[0] <= T){
        cout << T << endl;
        return 0;
    }
    long long record = -1;
    for (int r = 0; r < N; ++r){
        long long t = max(distP[r], distQ[r]);
        if ((dist0[r] + t) * 2 <= T) record = max(record, T - t * 2);
    }
    cout << record << endl;
    return 0;
}
0