結果
問題 | No.2764 Warp Drive Spacecraft |
ユーザー |
|
提出日時 | 2024-05-17 22:21:39 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 405 ms / 3,000 ms |
コード長 | 3,719 bytes |
コンパイル時間 | 1,138 ms |
コンパイル使用メモリ | 92,876 KB |
実行使用メモリ | 33,900 KB |
最終ジャッジ日時 | 2024-07-17 20:23:38 |
合計ジャッジ時間 | 9,226 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 35 |
ソースコード
#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 secondusing 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;elser = 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.firstlong t=Q.query_monotone_inc(x.first);ans=min(ans,t+x.second);}cout<<ans<<endl;}