結果

問題 No.848 なかよし旅行
ユーザー risujirohrisujiroh
提出日時 2019-07-05 22:13:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 95 ms / 2,000 ms
コード長 1,349 bytes
コンパイル時間 1,616 ms
コンパイル使用メモリ 178,072 KB
実行使用メモリ 8,788 KB
最終ジャッジ日時 2023-08-07 19:41:12
合計ジャッジ時間 3,560 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
8,788 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,388 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 18 ms
4,608 KB
testcase_12 AC 22 ms
4,684 KB
testcase_13 AC 28 ms
5,192 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 25 ms
5,072 KB
testcase_16 AC 41 ms
5,752 KB
testcase_17 AC 28 ms
5,360 KB
testcase_18 AC 17 ms
4,448 KB
testcase_19 AC 15 ms
4,380 KB
testcase_20 AC 10 ms
4,380 KB
testcase_21 AC 33 ms
5,424 KB
testcase_22 AC 30 ms
5,452 KB
testcase_23 AC 10 ms
4,380 KB
testcase_24 AC 1 ms
4,508 KB
testcase_25 AC 43 ms
6,136 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

struct Edge { int to, cost; };

template<class T, class Edge> V<T> dijkstra(const VV<Edge>& g, int s = 0) {
  V<T> dist(g.size(), numeric_limits<T>::max());
  using P = pair<T, int>;
  priority_queue< P, V<P>, greater<P> > pque;
  pque.emplace(dist[s] = 0, s);
  while (!pque.empty()) {
    T d; int v;
    tie(d, v) = pque.top(); pque.pop();
    if (d > dist[v]) continue;
    for (const auto& e : g[v]) if (dist[v] + e.cost < dist[e.to]) {
      pque.emplace(dist[e.to] = dist[v] + e.cost, e.to);
    } 
  }
  return dist;
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, m, p, q, t; cin >> n >> m >> p >> q >> t, --p, --q;
  VV<Edge> g(n);
  while (m--) {
    int a, b, c; cin >> a >> b >> c, --a, --b;
    g[a].emplace_back(Edge{b, c});
    g[b].emplace_back(Edge{a, c});
  }
  auto d0 = dijkstra<lint>(g, 0);
  auto dp = dijkstra<lint>(g, p);
  auto dq = dijkstra<lint>(g, q);
  lint res = -1;
  for (int u = 0; u < n; ++u) for (int v = 0; v < n; ++v) {
    if (d0[u] + max(dp[u] + dp[v], dq[u] + dq[v]) + d0[v] > t) continue;
    res = max(res, t - max(dp[u] + dp[v], dq[u] + dq[v]));
  }
  if (d0[p] + dp[q] + dq[0] <= t) res = t;
  cout << res << '\n';
}
0