結果

問題 No.848 なかよし旅行
ユーザー kyunakyuna
提出日時 2019-10-20 15:56:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 1,500 bytes
コンパイル時間 957 ms
コンパイル使用メモリ 89,240 KB
実行使用メモリ 10,716 KB
最終ジャッジ日時 2024-04-25 14:09:24
合計ジャッジ時間 3,154 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 157 ms
10,716 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 36 ms
6,940 KB
testcase_12 AC 47 ms
6,944 KB
testcase_13 AC 64 ms
6,940 KB
testcase_14 AC 20 ms
6,940 KB
testcase_15 AC 60 ms
6,940 KB
testcase_16 AC 105 ms
8,448 KB
testcase_17 AC 73 ms
7,296 KB
testcase_18 AC 35 ms
6,940 KB
testcase_19 AC 30 ms
6,944 KB
testcase_20 AC 13 ms
6,940 KB
testcase_21 AC 88 ms
7,424 KB
testcase_22 AC 96 ms
7,424 KB
testcase_23 AC 11 ms
6,940 KB
testcase_24 AC 1 ms
6,940 KB
testcase_25 AC 111 ms
8,448 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
const long long INF = 1LL << 60;    // 1.15x10^18
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

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

vector<long long> dijkstra(const Graph &g, int s) {
    vector<long long> dist(g.size(), INF);
    using Pi = pair<long long, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    dist[s] = 0; que.emplace(dist[s], s);
    while (!que.empty()) {
        long long cost; int u; tie(cost, u) = que.top(); que.pop();
        if (dist[u] < cost) continue;
        for (auto &e: g[u]) {
            int v; long long nc; tie(v, nc) = e;
            if (chmin(dist[v], dist[u] + nc)) que.emplace(dist[v], v);
        }
    }
    return dist;
}

int main() {
    int N, M, P, Q, T; cin >> N >> M >> P >> Q >> T; P--, Q--;
    Graph g(N);
    while (M--) {
        int a, b, c; cin >> a >> b >> c; a--, b--;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    auto d = dijkstra(g, 0);
    auto p = dijkstra(g, P);
    auto q = dijkstra(g, Q);
    if (d[P] + p[Q] + q[0] <= T) return !(cout << T << endl);
    int ans = -1;
    for (int u = 0; u < N; u++) for (int v = 0; v < N; v++) {
        int alone = max(p[u] + p[v], q[u] + q[v]);
        if (d[u] + alone + d[v] <= T) ans = max(ans, T - alone);
    }
    cout << ans << endl;
    return 0;
}
0