結果

問題 No.848 なかよし旅行
ユーザー 0w10w1
提出日時 2019-07-05 22:46:27
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,432 bytes
コンパイル時間 2,474 ms
コンパイル使用メモリ 211,420 KB
実行使用メモリ 13,712 KB
最終ジャッジ日時 2023-08-07 19:42:02
合計ジャッジ時間 4,311 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
13,712 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 19 ms
6,008 KB
testcase_12 AC 25 ms
6,996 KB
testcase_13 AC 33 ms
7,988 KB
testcase_14 AC 14 ms
4,532 KB
testcase_15 AC 30 ms
7,832 KB
testcase_16 AC 50 ms
11,000 KB
testcase_17 AC 34 ms
8,756 KB
testcase_18 AC 19 ms
5,772 KB
testcase_19 AC 17 ms
5,332 KB
testcase_20 AC 10 ms
4,376 KB
testcase_21 AC 40 ms
9,644 KB
testcase_22 AC 40 ms
10,428 KB
testcase_23 AC 10 ms
4,380 KB
testcase_24 AC 1 ms
4,504 KB
testcase_25 AC 53 ms
11,040 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<long long> dijkstra(vector<vector<pair<long long, int>>> E, int S) {
  const long long INF = 1LL << 50;
  int n = E.size();
  vector<long long> dis(n, INF);
  priority_queue<pair<long long, int>> pq;
  pq.emplace(dis[S] = 0, S);
  while (pq.size()) {
    int d, u;
    tie(d, u) = pq.top();
    d *= -1;
    pq.pop();
    if (d != dis[u]) continue;
    for (auto e : E[u]) {
      long long w;
      int v;
      tie(w, v) = e;
      if (dis[v] <= dis[u] + w) continue;
      dis[v] = dis[u] + w;
      pq.emplace(-dis[v], v);
    }
  }
  return dis;
}

signed main() {
  ios::sync_with_stdio(false);

  int N, M, P, Q, T;
  cin >> N >> M >> P >> Q >> T;
  --P;
  --Q;

  vector<vector<pair<long long, int>>> G(N);
  for (int i = 0; i < M; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    G[u - 1].emplace_back(w, v - 1);
    G[v - 1].emplace_back(w, u - 1);
  }

  vector<long long> pdis = dijkstra(G, P);
  vector<long long> qdis = dijkstra(G, Q);
  vector<long long> odis = dijkstra(G, 0);

  int ans = -1;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      long long t = odis[i] + max(pdis[i] + pdis[j], qdis[i] + qdis[j]) + odis[j];
      if (t <= T) {
        int w = T - max(pdis[i] + pdis[j], qdis[i] + qdis[j]);
        ans = max(ans, w);
      }
    }
  }
  if (odis[P] + pdis[Q] + qdis[0] <= T) ans = T;

  cout << ans << endl;

  return 0;
}
0