結果

問題 No.848 なかよし旅行
コンテスト
ユーザー kyuna
提出日時 2019-07-27 23:47:42
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,827 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 426 ms
コンパイル使用メモリ 86,208 KB
最終ジャッジ日時 2026-05-10 05:38:25
合計ジャッジ時間 1,057 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
main.cpp: In function 'std::vector<_Tp> dijkstra(WeightedGraph<T>&, int)':
main.cpp:19:19: error: 'numeric_limits' was not declared in this scope [-Wtemplate-body]
   19 |     const T INF = numeric_limits<T>::max();
      |                   ^~~~~~~~~~~~~~
main.cpp:19:35: error: expected primary-expression before '>' token [-Wtemplate-body]
   19 |     const T INF = numeric_limits<T>::max();
      |                                   ^
main.cpp:19:41: error: no matching function for call to 'max()' [-Wtemplate-body]
   19 |     const T INF = numeric_limits<T>::max();
      |                                    ~~~~~^~
main.cpp:19:41: note: there are 4 candidates
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/algorithm:62,
                 from main.cpp:1:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:258:5: note: candidate 1: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  258 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:258:5: note: candidate expects 2 arguments, 0 provided
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:304:5: note: candidate 2: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  304 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algobase.h:304:5: note: candidate expects 3 arguments, 0 provided
In file included from /home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/algorithm:63:
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c++/15/bits/stl_algo.h:5784:5: note: candidate 3: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5784 |     max(initializer_list<_Tp> __l)
      |     ^~~
/home/linuxbrew/.linuxbrew/Cellar/gcc/15.2.0_1/include/c

ソースコード

diff #
raw source code

#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;

template <typename T>
struct Edge { int src, dst; T cost;
    Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { }
    Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { }
};
template <typename T> using Edges = vector<Edge<T>>;
template <typename T> using WeightedGraph = vector<Edges<T>>;
template <typename T> using Matrix = vector<vector<T>>;

template <typename T>
vector<T> dijkstra(const WeightedGraph<T> &g, int s) {
    const T INF = numeric_limits<T>::max();
    vector<T> dist(g.size(), INF);
    vector<int> prev(g.size(), -1);
    using Pi = pair<T, int>;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    dist[s] = 0; que.emplace(dist[s], s);
    while (!que.empty()) {
        T cost; int u; tie(cost, u) = que.top(); que.pop();
        if (dist[u] < cost) continue;
        for (auto &e: g[u]) {
            int v = e.dst; T nc = e.cost;
            if (dist[v] <= dist[u] + nc) continue;
            dist[v] = dist[u] + nc; prev[v] = u;
            que.emplace(dist[v], v);
        }
    }
    return dist;
}

int main() {
    int N, M, P, Q, T; cin >> N >> M >> P >> Q >> T; P--, Q--;
    WeightedGraph<long long> 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);
    }
    vector<long long> d, p, q;
    d = dijkstra(g, 0);
    p = dijkstra(g, P);
    q = dijkstra(g, Q);
    if (d[P] + p[Q] + q[0] <= T) return cout << T << endl, 0;
    int ans = -1;
    for (int s = 0; s < N; s++) for (int t = 0; t < N; t++) {
        int alone = max(p[s] + p[t], q[s] + q[t]);
        if (d[s] + alone + d[t] <= T) ans = max(ans, T - alone);
    }
    cout << ans << endl;
    return 0;
}
0