結果

問題 No.848 なかよし旅行
ユーザー hrbt__hrbt__
提出日時 2019-07-24 11:55:33
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 2,417 bytes
コンパイル時間 1,636 ms
コンパイル使用メモリ 177,564 KB
実行使用メモリ 10,728 KB
最終ジャッジ日時 2023-08-07 19:49:54
合計ジャッジ時間 3,975 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
10,728 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 33 ms
5,192 KB
testcase_12 AC 43 ms
5,560 KB
testcase_13 AC 58 ms
6,480 KB
testcase_14 AC 18 ms
4,376 KB
testcase_15 AC 54 ms
6,572 KB
testcase_16 AC 92 ms
8,068 KB
testcase_17 AC 65 ms
7,256 KB
testcase_18 AC 32 ms
5,040 KB
testcase_19 AC 27 ms
4,616 KB
testcase_20 AC 12 ms
4,376 KB
testcase_21 AC 80 ms
7,308 KB
testcase_22 AC 85 ms
7,352 KB
testcase_23 AC 11 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 100 ms
8,056 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define dmp(x) cerr << __LINE__ << " " << #x << " " << x << endl;
#define line(obj) for (auto itr = begin(obj); itr != end(obj); itr++) s << ((itr == begin(obj)) ? "" : ", ") << *itr
template <typename T1, typename T2>
ostream& operator<<(ostream& s, const pair<T1, T2>& p) {
  s << "(";
  s << p.first << ", " << p.second;
  s << ")";
  return s;
}
template <typename T>
ostream& operator<<(ostream& s, const vector<T>& v) {
  s << "[";
  line(v);
  s << "]";
  return s;
}
template <typename T>
ostream& operator<<(ostream& s, const vector<vector<T>>& vv) {
  s << "[";
  line(vv);
  s << "]";
  return s;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& s, const map<T1, T2>& m) {
  s << "{";
  line(m);
  s << "}";
  return s;
}

template <typename T>
struct Dijkstra {
  struct Edge {
    int to;
    T cost;
  };
  vector<int> prev;
  vector<vector<Edge>> g;
  Dijkstra(int n) : prev(n, -1), g(n) {}

  void addEdge(int u, int v, T w) {
    g[u].push_back({v, w});
    g[v].push_back({u, w});
  }
  vector<T> build(int s) {
    vector<T> dist(g.size(), -1);
    using Node = pair<T, int>;
    priority_queue<Node, vector<Node>, greater<Node>> pq;
    pq.push({dist[s] = 0, s});

    while (!pq.empty()) {
      auto d = pq.top().first;
      auto u = pq.top().second;
      pq.pop();
      if (dist[u] < d) continue;
      for (auto&& v : g[u]) {
        if (dist[v.to] < 0 || dist[v.to] > dist[u] + v.cost) {
          dist[v.to] = dist[u] + v.cost;
          prev[v.to] = u;
          pq.push({dist[v.to], v.to});
        }
      }
    }
    return dist;
  }
  vector<int> getPath(int t) {
    vector<int> path;
    for (; t != -1; t = prev[t]) path.push_back(t);
    reverse(begin(path), end(path));
    return path;
  }
};

int main() {
  int n, m, p, q, t;
  cin >> n >> m >> p >> q >> t, p--, q--;
  Dijkstra<ll> g(n);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c, a--, b--;
    g.addEdge(a, b, c);
  }
  auto zs = g.build(0);
  auto ps = g.build(p);
  auto qs = g.build(q);
  if (zs[p] + zs[q] + ps[q] <= t) {
    cout << t << endl;
    return 0;
  }
  ll ans = -1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      ll mx = max(ps[i] + ps[j], qs[i] + qs[j]);
      if (zs[i] + zs[j] + mx > t) continue;
      ans = max(ans, t - mx);
    }
  }
  cout << ans << endl;
}
0