結果

問題 No.848 なかよし旅行
ユーザー pekempeypekempey
提出日時 2019-07-05 22:06:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 1,421 bytes
コンパイル時間 1,888 ms
コンパイル使用メモリ 174,624 KB
実行使用メモリ 10,620 KB
最終ジャッジ日時 2024-04-25 13:48:08
合計ジャッジ時間 3,326 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
10,620 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 3 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 22 ms
6,940 KB
testcase_12 AC 26 ms
6,944 KB
testcase_13 AC 34 ms
6,944 KB
testcase_14 AC 14 ms
6,940 KB
testcase_15 AC 32 ms
6,944 KB
testcase_16 AC 53 ms
8,064 KB
testcase_17 AC 33 ms
7,168 KB
testcase_18 AC 22 ms
6,944 KB
testcase_19 AC 21 ms
6,944 KB
testcase_20 AC 14 ms
6,944 KB
testcase_21 AC 41 ms
7,040 KB
testcase_22 AC 36 ms
7,424 KB
testcase_23 AC 20 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 59 ms
8,064 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)

using namespace std;
using ll = long long;

struct edge {
  int v;
  ll w;
};

vector<ll> distance(const vector<vector<edge>> &g, int s) {
  const int n = g.size();
  priority_queue<pair<ll, int>> q;
  q.emplace(0, s);
  vector<ll> dist(n, 1e18);
  dist[s] = 0;
  while (!q.empty()) {
    int u = q.top().second;
    ll d = -q.top().first;
    q.pop();
    if (dist[u] < d) continue;
    for (edge e : g[u]) {
      if (dist[e.v] > dist[u] + e.w) {
        dist[e.v] = dist[u] + e.w;
        q.emplace(-dist[e.v], e.v);
      }
    }
  }
  return dist;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N, M, P, Q;
  ll T;
  cin >> N >> M >> P >> Q >> T;
  P--; Q--;
  vector<vector<edge>> g(N);
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    a--; b--;
    g[a].push_back((edge){b, c});
    g[b].push_back((edge){a, c});
  }
  vector<ll> distP = distance(g, P);
  vector<ll> distQ = distance(g, Q);
  vector<ll> dist0 = distance(g, 0);
  ll ans = -1;
  if (dist0[P] + distP[Q] + distQ[0] <= T) {
    ans = max(ans, T);
  }
  rep(i, N) {
    rep(j, N) {
      if (dist0[i] + dist0[j] + max(distP[i] + distP[j], distQ[i] + distQ[j]) <= T) {
        ans = max(ans, T - max(distP[i] + distP[j], distQ[i] + distQ[j]));
      }
    }
  }
  cout << ans << endl;
}
0