結果

問題 No.2764 Warp Drive Spacecraft
ユーザー shobonvipshobonvip
提出日時 2024-05-17 22:45:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 501 ms / 3,000 ms
コード長 4,558 bytes
コンパイル時間 4,658 ms
コンパイル使用メモリ 274,948 KB
実行使用メモリ 26,044 KB
最終ジャッジ日時 2024-07-17 20:24:04
合計ジャッジ時間 14,296 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 254 ms
21,908 KB
testcase_17 AC 261 ms
21,976 KB
testcase_18 AC 245 ms
22,180 KB
testcase_19 AC 361 ms
20,048 KB
testcase_20 AC 370 ms
21,444 KB
testcase_21 AC 367 ms
20,384 KB
testcase_22 AC 381 ms
21,248 KB
testcase_23 AC 365 ms
20,192 KB
testcase_24 AC 386 ms
21,588 KB
testcase_25 AC 373 ms
21,276 KB
testcase_26 AC 476 ms
25,208 KB
testcase_27 AC 471 ms
25,208 KB
testcase_28 AC 487 ms
25,616 KB
testcase_29 AC 489 ms
25,596 KB
testcase_30 AC 464 ms
25,128 KB
testcase_31 AC 485 ms
26,044 KB
testcase_32 AC 501 ms
26,028 KB
testcase_33 AC 241 ms
15,444 KB
testcase_34 AC 239 ms
15,452 KB
testcase_35 AC 240 ms
15,348 KB
testcase_36 AC 272 ms
22,044 KB
testcase_37 AC 247 ms
19,484 KB
testcase_38 AC 209 ms
17,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

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

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}

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); }
};

vector<ll> dijk(int st, int n, vector<vector<pair<int,ll>>> &ikeru){
	vector<ll> d(n,1e18);
	priority_queue<pair<ll,int>> pq;
	pq.push(pair(0, st));
	d[st] = 0;

	while(!pq.empty()){
		auto [tmp, i] = pq.top();
		pq.pop();
		tmp = -tmp;
		if(tmp>d[i]) continue;
		for (auto [j,c]: ikeru[i]){
			if (chmin(d[j], tmp+c)){
				pq.push(pair(-d[j], j));
			}
		}
	}
	return d;
}

int main(){
	int n, m; cin >> n >> m;
	vector<ll> w(n);
	rep(i,0,n){
		cin >> w[i];
	}

	vector ikeru(n, vector<pair<int,ll>>(0));
	rep(i,0,m){
		int u, v; cin >> u >> v;
		u--; v--;
		ll t; cin >> t;
		ikeru[u].push_back(pair(v, t));
		ikeru[v].push_back(pair(u, t));
	}

	vector<ll> d1 = dijk(0, n, ikeru);
	vector<ll> d2 = dijk(n-1, n, ikeru);

	DynamicLiChaoTree<ll, (ll)0, (ll)1e9, (ll)4e18> lichao;
	rep(i,0,n){
		if(d2[i] != (ll)1e18){
			lichao.add_line(w[i], d2[i]);
		}
	}
	ll ans = d1[n-1];
	rep(i,0,n){
		if(d1[i] != (ll)1e18){
			chmin(ans, lichao.query(w[i]) + d1[i]);
		}
	}
	cout << ans << '\n';
}

0