結果

問題 No.848 なかよし旅行
ユーザー cotton_fn_cotton_fn_
提出日時 2019-07-05 22:12:53
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 2,149 bytes
コンパイル時間 1,299 ms
コンパイル使用メモリ 125,228 KB
実行使用メモリ 10,720 KB
最終ジャッジ日時 2024-04-25 13:48:44
合計ジャッジ時間 3,289 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
10,720 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 35 ms
6,940 KB
testcase_12 AC 46 ms
6,944 KB
testcase_13 AC 62 ms
6,944 KB
testcase_14 AC 18 ms
6,944 KB
testcase_15 AC 57 ms
6,944 KB
testcase_16 AC 101 ms
8,320 KB
testcase_17 AC 70 ms
7,296 KB
testcase_18 AC 33 ms
6,944 KB
testcase_19 AC 29 ms
6,940 KB
testcase_20 AC 10 ms
6,940 KB
testcase_21 AC 85 ms
7,552 KB
testcase_22 AC 95 ms
7,296 KB
testcase_23 AC 20 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 108 ms
8,320 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 1 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <deque>
#include <queue>
#include <array>
#include <set>
#include <map>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cstdint>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;
template<class T, class U> void init_n(vector<T>& v, size_t n, U x) 
{ v = vector<T>(n, x); }
template<class T> void init_n(vector<T>& v, size_t n) { init_n(v, n, T()); }
template<class T> void read_n(vector<T>& v, size_t n, size_t o = 0) 
{ v = vector<T>(n+o); for (size_t i=o; i<n+o; ++i) cin >> v[i]; }
template<class T> void read_n(T a[], size_t n, size_t o = 0)
{ for (size_t i=o; i<n+o; ++i) cin >> a[i]; }
template<class T> T gabs(const T& x) { return max(x, -x); }
#define abs gabs

template<class T>
vector<i64> djk(const T& e, i64 n, i64 s) {
  vector<i64> d(n + 1, 1e18);
  vector<bool> fix(n + 1);

  using P = pair<i64, i64>;
  priority_queue<P, vector<P>, greater<P>> pq;
  d[s] = 0;
  pq.emplace(0, s);
  while (!pq.empty()) {
    P p = pq.top(); pq.pop();
    i64 u = p.second;
    if (fix[u]) continue;
    fix[u] = true;
    for (const P& q : e[u]) {
      i64 cv = q.first, v = q.second;
      if (!fix[v] && d[u] + cv < d[v]) {
        pq.emplace(d[u] + cv, v);
        d[v] = d[u] + cv;
      }
    }
  }
  return d;
}

i64 n, m, p, q, t;
vector<vector<pair<i64, i64>>> g;
int main() {
  cin >> n >> m >> p >> q >> t;
  init_n(g, n + 1);
  for (i64 i = 0; i < m; ++i) {
    i64 a, b, c;
    cin >> a >> b >> c;
    g[a].emplace_back(c, b);
    g[b].emplace_back(c, a);
  }
  
  auto d1 = djk(g, n, 1);
  auto dp = djk(g, n, p);
  auto dq = djk(g, n, q);

  bool ok = false;
  i64 ans = 0;
  if (d1[p] + dp[q] + dq[1] <= t) {
    ans = t;
    ok = true;
  }
  for (i64 u = 1; u <= n; ++u) {
    for (i64 v = 1; v <= n; ++v) {
      i64 x = max(dp[u] + dp[v], dq[u] + dq[v]);
      if (d1[u] + d1[v] + x <= t) {
        ans = max(ans, t - x);
        ok = true;
      }
    }
  }

  if (!ok) ans = -1;
  cout << ans << endl;
  return 0;
}
0