結果

問題 No.2764 Warp Drive Spacecraft
ユーザー KudeKude
提出日時 2024-05-17 23:18:43
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 372 ms / 3,000 ms
コード長 4,509 bytes
コンパイル時間 3,547 ms
コンパイル使用メモリ 286,836 KB
実行使用メモリ 25,288 KB
最終ジャッジ日時 2024-05-18 00:11:38
合計ジャッジ時間 10,959 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 3 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 105 ms
21,268 KB
testcase_17 AC 107 ms
21,188 KB
testcase_18 AC 106 ms
21,080 KB
testcase_19 AC 250 ms
19,560 KB
testcase_20 AC 248 ms
20,956 KB
testcase_21 AC 262 ms
19,772 KB
testcase_22 AC 258 ms
20,892 KB
testcase_23 AC 249 ms
19,828 KB
testcase_24 AC 256 ms
20,960 KB
testcase_25 AC 257 ms
20,780 KB
testcase_26 AC 361 ms
24,508 KB
testcase_27 AC 341 ms
24,268 KB
testcase_28 AC 355 ms
24,900 KB
testcase_29 AC 354 ms
24,708 KB
testcase_30 AC 355 ms
24,380 KB
testcase_31 AC 354 ms
25,288 KB
testcase_32 AC 372 ms
25,276 KB
testcase_33 AC 115 ms
15,244 KB
testcase_34 AC 117 ms
15,156 KB
testcase_35 AC 119 ms
15,164 KB
testcase_36 AC 157 ms
21,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;


constexpr long long INF = 1002003004005006007;
vector<long long> dijkstra(const auto& to, int s = 0) {
  struct QueElem {
    int v;
    long long d;
    bool operator<(const QueElem& a) const { return d > a.d; }
    QueElem(int v, long long d) : v(v), d(d) {}
  };
  priority_queue<QueElem> q;
  vector<long long> dist(to.size(), INF);
  dist[s] = 0;
  q.emplace(s, 0);
  while (!q.empty()) {
    auto [u, d] = q.top(); q.pop();
    if (d > dist[u]) continue;
    for (auto [v, dv] : to[u]) {
      long long nd = d + dv;
      if (nd < dist[v]) {
        dist[v] = nd;
        q.emplace(v, nd);
      }
    }
  }
  return dist;
}


/**
 * @brief Dynamic-Li-Chao-Tree
 *
 */
template <typename T, T x_low, T x_high, T id>
struct DynamicLiChaoTree {
  struct Line {
    T a, b;

    Line(T a, T b) : a(a), b(b) {}

    inline T get(T x) const { return a * x + b; }
  };

  struct Node {
    Line x;
    Node *l, *r;

    Node(const Line &x) : x{x}, l{nullptr}, r{nullptr} {}
  };

  Node *root;

  DynamicLiChaoTree() : root{nullptr} {}

  Node *add_line(Node *t, Line &x, const T &l, const T &r, const T &x_l,
                 const T &x_r) {
    if (!t) return new Node(x);

    T t_l = t->x.get(l), t_r = t->x.get(r);

    if (t_l <= x_l && t_r <= x_r) {
      return t;
    } else if (t_l >= x_l && t_r >= x_r) {
      t->x = x;
      return t;
    } else {
      T m = (l + r) / 2;
      if (m == r) --m;
      T t_m = t->x.get(m), x_m = x.get(m);
      if (t_m > x_m) {
        swap(t->x, x);
        if (x_l >= t_l)
          t->l = add_line(t->l, x, l, m, t_l, t_m);
        else
          t->r = add_line(t->r, x, m + 1, r, t_m + x.a, t_r);
      } else {
        if (t_l >= x_l)
          t->l = add_line(t->l, x, l, m, x_l, x_m);
        else
          t->r = add_line(t->r, x, m + 1, r, x_m + x.a, x_r);
      }
      return t;
    }
  }

  void add_line(const T &a, const T &b) {
    Line x(a, b);
    root = add_line(root, x, x_low, x_high, x.get(x_low), x.get(x_high));
  }

  Node *add_segment(Node *t, Line &x, const T &a, const T &b, const T &l,
                    const T &r, const T &x_l, const T &x_r) {
    if (r < a || b < l) return t;
    if (a <= l && r <= b) {
      Line y{x};
      return add_line(t, y, l, r, x_l, x_r);
    }
    if (t) {
      T t_l = t->x.get(l), t_r = t->x.get(r);
      if (t_l <= x_l && t_r <= x_r) return t;
    } else {
      t = new Node(Line(0, id));
    }
    T m = (l + r) / 2;
    if (m == r) --m;
    T x_m = x.get(m);
    t->l = add_segment(t->l, x, a, b, l, m, x_l, x_m);
    t->r = add_segment(t->r, x, a, b, m + 1, r, x_m + x.a, x_r);
    return t;
  }

  void add_segment(const T &l, const T &r, const T &a, const T &b) {
    Line x(a, b);
    root = add_segment(root, x, l, r - 1, x_low, x_high, x.get(x_low),
                       x.get(x_high));
  }

  T query(const Node *t, const T &l, const T &r, const T &x) const {
    if (!t) return id;
    if (l == r) return t->x.get(x);
    T m = (l + r) / 2;
    if (m == r) --m;
    if (x <= m)
      return min(t->x.get(x), query(t->l, l, m, x));
    else
      return min(t->x.get(x), query(t->r, m + 1, r, x));
  }

  T query(const T &x) const { return query(root, x_low, x_high, x); }
};


} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  VI w(n);
  rep(i, n) cin >> w[i];
  vector<vector<pair<int, ll>>> to(n);
  rep(_, m) {
    int u, v;
    ll t;
    cin >> u >> v >> t;
    u--, v--;
    to[u].emplace_back(v, t);
    to[v].emplace_back(u, t);
  }
  auto dist1 = dijkstra(to);
  auto dist2 = dijkstra(to, n - 1);
  DynamicLiChaoTree<ll, 0, (ll)1e9 + 1, (ll)4e18> cht;
  rep(i, n) cht.add_line(w[i], dist2[i]);
  ll ans = dist1[n - 1];
  rep(i, n) chmin(ans, dist1[i] + cht.query(w[i]));
  cout << ans << '\n';
}
0