結果

問題 No.848 なかよし旅行
ユーザー ikdikd
提出日時 2019-07-05 23:47:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,234 ms / 2,000 ms
コード長 1,435 bytes
コンパイル時間 832 ms
コンパイル使用メモリ 89,460 KB
実行使用メモリ 13,884 KB
最終ジャッジ日時 2024-04-25 13:53:32
合計ジャッジ時間 5,022 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,234 ms
13,884 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 2 ms
6,812 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 35 ms
6,940 KB
testcase_12 AC 44 ms
7,168 KB
testcase_13 AC 61 ms
8,320 KB
testcase_14 AC 17 ms
6,940 KB
testcase_15 AC 58 ms
7,880 KB
testcase_16 AC 102 ms
11,264 KB
testcase_17 AC 73 ms
8,832 KB
testcase_18 AC 33 ms
6,940 KB
testcase_19 AC 28 ms
6,944 KB
testcase_20 AC 10 ms
6,944 KB
testcase_21 AC 89 ms
9,600 KB
testcase_22 AC 102 ms
10,380 KB
testcase_23 AC 20 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 111 ms
11,264 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <tuple>

using namespace std;
struct Edge{
  int to;
  int64_t cost;
  Edge(int to, int64_t cost): to(to), cost(cost) {}
};

const int64_t inf = 1e18;
vector<int64_t> dijkstra(vector<vector<Edge>> g, int s) {
  int n = g.size();
  vector<int64_t> d(n, inf);
  d[s] = 0;
  priority_queue<pair<int64_t, int>> q;
  q.emplace(0, s);
  while (q.size() > 0) {
    int64_t dist;
    int v;
    tie(dist, v) = q.top();
    q.pop();
    dist *= -1;
    for (const auto &e: g[v]) {
      if (dist + e.cost < d[e.to]) {
        d[e.to] = dist + e.cost;
        q.emplace(d[e.to] * (-1), e.to);
      }
    }
  }
  return d;
}

int main() {
  
  int n, m, p, q;
  int64_t t;
  cin >> n >> m >> p >> q >> t;
  p--;
  q--;
  vector<vector<Edge>> g(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    g[a].emplace_back(b, c);
    g[b].emplace_back(a, c);
  }

  auto d0 = dijkstra(g, 0),
       dp = dijkstra(g, p),
       dq = dijkstra(g, q);
  int64_t ans = -1;
  for (int v = 0; v < n; v++) {
    if ((d0[v] + dp[v] + dq[v]) * 2 <= t) {
      ans = max(ans, t);
    }
    for (int u = 0; u < n; u++) {
      auto time = t - d0[v] - d0[u];
      time -= max(dp[v] + dp[u], dq[v] + dq[u]);
      if (time >= 0) {
        ans = max(ans, d0[v] + d0[u] + time);
      }
    } 
  } 

  cout << ans << endl;
  return 0;
}
0