結果

問題 No.2764 Warp Drive Spacecraft
ユーザー kotatsugamekotatsugame
提出日時 2024-05-17 22:21:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 422 ms / 3,000 ms
コード長 3,719 bytes
コンパイル時間 1,192 ms
コンパイル使用メモリ 93,212 KB
実行使用メモリ 34,140 KB
最終ジャッジ日時 2024-05-18 00:10:25
合計ジャッジ時間 9,513 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
9,524 KB
testcase_01 AC 5 ms
9,556 KB
testcase_02 AC 5 ms
9,496 KB
testcase_03 AC 4 ms
9,572 KB
testcase_04 AC 5 ms
9,664 KB
testcase_05 AC 5 ms
9,532 KB
testcase_06 AC 5 ms
9,612 KB
testcase_07 AC 5 ms
9,684 KB
testcase_08 AC 5 ms
9,912 KB
testcase_09 AC 5 ms
9,580 KB
testcase_10 AC 6 ms
9,656 KB
testcase_11 AC 5 ms
9,596 KB
testcase_12 AC 5 ms
9,924 KB
testcase_13 AC 6 ms
9,548 KB
testcase_14 AC 5 ms
9,640 KB
testcase_15 AC 4 ms
9,756 KB
testcase_16 AC 191 ms
31,464 KB
testcase_17 AC 193 ms
31,540 KB
testcase_18 AC 192 ms
32,104 KB
testcase_19 AC 278 ms
26,768 KB
testcase_20 AC 288 ms
27,092 KB
testcase_21 AC 293 ms
27,064 KB
testcase_22 AC 289 ms
27,056 KB
testcase_23 AC 282 ms
26,784 KB
testcase_24 AC 291 ms
26,960 KB
testcase_25 AC 291 ms
26,920 KB
testcase_26 AC 401 ms
32,748 KB
testcase_27 AC 392 ms
33,208 KB
testcase_28 AC 399 ms
33,120 KB
testcase_29 AC 404 ms
34,140 KB
testcase_30 AC 401 ms
33,640 KB
testcase_31 AC 408 ms
33,720 KB
testcase_32 AC 422 ms
33,316 KB
testcase_33 AC 131 ms
21,608 KB
testcase_34 AC 127 ms
21,616 KB
testcase_35 AC 130 ms
21,496 KB
testcase_36 AC 167 ms
32,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cassert>
using namespace std;
//https://ei1333.github.io/library/structure/convex-hull-trick/convex-hull-trick-add-monotone.hpp.html
/**
 * @brief Convex Hull Trick Add Monotone
 *
 */
template <typename T, bool isMin>
struct ConvexHullTrickAddMonotone {
#define F first
#define S second
  using P = pair<T, T>;
  deque<P> H;

  ConvexHullTrickAddMonotone() = default;

  bool empty() const { return H.empty(); }

  void clear() { H.clear(); }

  inline int sgn(T x) { return x == 0 ? 0 : (x < 0 ? -1 : 1); }

  inline bool check(const P &a, const P &b, const P &c) {
    if (b.S == a.S || c.S == b.S)
      return sgn(b.F - a.F) * sgn(c.S - b.S) >= sgn(c.F - b.F) * sgn(b.S - a.S);
    // return (b.F-a.F)*(c.S-b.S) >= (b.S-a.S)*(c.F-b.F);
    if (is_integral<T>::value) {
      return (b.S - a.S) / (a.F - b.F) >= (c.S - b.S) / (b.F - c.F);
    } else {
      return (b.F - a.F) * sgn(c.S - b.S) / abs(b.S - a.S) >=
             (c.F - b.F) * sgn(b.S - a.S) / abs(c.S - b.S);
    }
  }

  void add(T a, T b) {
    if (!isMin) a *= -1, b *= -1;
    P line(a, b);
    if (empty()) {
      H.emplace_front(line);
      return;
    }
    if (H.front().F <= a) {
      if (H.front().F == a) {
        if (H.front().S <= b) return;
        H.pop_front();
      }
      while (H.size() >= 2 && check(line, H.front(), H[1])) H.pop_front();
      H.emplace_front(line);
    } else {
      assert(a <= H.back().F);
      if (H.back().F == a) {
        if (H.back().S <= b) return;
        H.pop_back();
      }
      while (H.size() >= 2 && check(H[H.size() - 2], H.back(), line))
        H.pop_back();
      H.emplace_back(line);
    }
  }

  inline T get_y(const P &a, const T &x) { return a.F * x + a.S; }

  T query(T x) {
    assert(!empty());
    int l = -1, r = H.size() - 1;
    while (l + 1 < r) {
      int m = (l + r) >> 1;
      if (get_y(H[m], x) >= get_y(H[m + 1], x))
        l = m;
      else
        r = m;
    }
    if (isMin) return get_y(H[r], x);
    return -get_y(H[r], x);
  }

  T query_monotone_inc(T x) {
    assert(!empty());
    while (H.size() >= 2 && get_y(H.front(), x) >= get_y(H[1], x))
      H.pop_front();
    if (isMin) return get_y(H.front(), x);
    return -get_y(H.front(), x);
  }

  T query_monotone_dec(T x) {
    assert(!empty());
    while (H.size() >= 2 && get_y(H.back(), x) >= get_y(H[H.size() - 2], x))
      H.pop_back();
    if (isMin) return get_y(H.back(), x);
    return -get_y(H.back(), x);
  }

#undef F
#undef S
};
int N,M;
int W[2<<17];
vector<pair<int,long> >G[2<<17];
long ans=9e18;
vector<pair<int,long> >f(int s)
{
	vector<long>ret(N,(long)1e18);
	ret[s]=0;
	priority_queue<pair<long,int> >Q;
	Q.push(make_pair(0L,s));
	while(!Q.empty())
	{
		int u=Q.top().second;
		long d=-Q.top().first;
		Q.pop();
		if(ret[u]<d)continue;
		for(pair<int,long>e:G[u])
		{
			int v=e.first;
			if(ret[v]>d+e.second)
			{
				ret[v]=d+e.second;
				Q.push(make_pair(-ret[v],v));
			}
		}
	}
	if(s==0)ans=min(ans,ret[N-1]);
	vector<pair<int,long> >A;
	for(int i=0;i<N;i++)if(ret[i]<(long)1e18)A.push_back(make_pair(W[i],ret[i]));
	sort(A.begin(),A.end());
	return A;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N>>M;
	for(int i=0;i<N;i++)cin>>W[i];
	for(int i=0;i<M;i++)
	{
		int u,v;
		long t;
		cin>>u>>v>>t;u--,v--;
		G[u].push_back(make_pair(v,t));
		G[v].push_back(make_pair(u,t));
	}
	vector<pair<int,long> >X=f(0),Y=f(N-1);
	ConvexHullTrickAddMonotone<long,true>Q;
	for(pair<int,long>f:Y)Q.add(f.first,f.second);
	for(pair<int,long>x:X)
	{//cost=x.second+y.second+x.first*y.first
		long t=Q.query_monotone_inc(x.first);
		ans=min(ans,t+x.second);
	}
	cout<<ans<<endl;
}
0