結果

問題 No.848 なかよし旅行
ユーザー ninjaninja
提出日時 2019-07-05 22:34:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,570 bytes
コンパイル時間 2,018 ms
コンパイル使用メモリ 180,364 KB
実行使用メモリ 54,412 KB
最終ジャッジ日時 2024-04-16 07:50:17
合計ジャッジ時間 8,664 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;

ll INF = 1010101010101010LL;
vector<vector<pair<int, ll>>> ps;

struct N {
  ll i, d;

  N(int i, ll d) : i(i), d(d) {}
};

bool operator<(const N &l, const N &r) {
  return l.d > r.d;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  ll n, m, p, q, t;
  cin >> n >> m >> p >> q >> t;
  p--;
  q--;

  vector<vector<ll>> cost(n, vector<ll>(n, INF));
  ps.resize(n);

  for (int i = 0; i < m; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    ps[a].emplace_back(b, c);
    ps[b].emplace_back(a, c);
  }

  priority_queue<N> queue;
  for (int i = 0; i < n; i++) {
    queue.emplace(i, 0);
    while (queue.size()) {
      auto v = queue.top();
      queue.pop();
      if (cost[i][v.i] < INF) continue;
      cost[i][v.i] = v.d;
      for (auto &path : ps[v.i]) {
        if (cost[i][path.first] < INF) continue;
        queue.emplace(path.first, v.d + path.second);
      }
    }
  }

  ll ans = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      ll pcost = cost[i][p] + cost[p][j];
      ll qcost = cost[i][q] + cost[q][j];
      ll ijcost = cost[0][i] + cost[j][0];
      if (max(pcost, qcost) + ijcost <= t) {
        ans = max(ans, t - max(pcost, qcost));
      }
      if (ijcost + pcost + qcost <= t) ans = t;
    }
  }

  if (cost[0][p] + cost[p][q] + cost[q][0] <= t) ans = t;

  cout << ans << endl;

  return 0;
}
0