結果

問題 No.848 なかよし旅行
ユーザー ikdikd
提出日時 2019-07-05 23:47:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 901 ms / 2,000 ms
コード長 1,435 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 88,608 KB
実行使用メモリ 13,844 KB
最終ジャッジ日時 2023-08-07 19:46:12
合計ジャッジ時間 4,676 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 901 ms
13,844 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 34 ms
6,120 KB
testcase_12 AC 44 ms
7,060 KB
testcase_13 AC 61 ms
8,100 KB
testcase_14 AC 19 ms
4,488 KB
testcase_15 AC 57 ms
7,820 KB
testcase_16 AC 97 ms
11,040 KB
testcase_17 AC 68 ms
8,884 KB
testcase_18 AC 33 ms
5,804 KB
testcase_19 AC 29 ms
5,380 KB
testcase_20 AC 13 ms
4,376 KB
testcase_21 AC 83 ms
9,400 KB
testcase_22 AC 91 ms
10,356 KB
testcase_23 AC 25 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 116 ms
10,916 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,380 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