結果

問題 No.2764 Warp Drive Spacecraft
ユーザー 👑 kmjpkmjp
提出日時 2024-05-20 23:03:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 319 ms / 3,000 ms
コード長 2,324 bytes
コンパイル時間 2,626 ms
コンパイル使用メモリ 222,704 KB
実行使用メモリ 34,116 KB
最終ジャッジ日時 2024-07-17 20:25:06
合計ジャッジ時間 8,999 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
8,192 KB
testcase_01 AC 3 ms
8,064 KB
testcase_02 AC 3 ms
8,192 KB
testcase_03 AC 3 ms
8,320 KB
testcase_04 AC 3 ms
8,064 KB
testcase_05 AC 3 ms
8,320 KB
testcase_06 AC 3 ms
8,064 KB
testcase_07 AC 2 ms
8,192 KB
testcase_08 AC 3 ms
8,320 KB
testcase_09 AC 3 ms
8,320 KB
testcase_10 AC 4 ms
8,192 KB
testcase_11 AC 3 ms
8,320 KB
testcase_12 AC 4 ms
8,192 KB
testcase_13 AC 3 ms
8,192 KB
testcase_14 AC 3 ms
8,064 KB
testcase_15 AC 2 ms
8,192 KB
testcase_16 AC 133 ms
28,376 KB
testcase_17 AC 135 ms
28,444 KB
testcase_18 AC 136 ms
28,456 KB
testcase_19 AC 203 ms
27,164 KB
testcase_20 AC 209 ms
29,204 KB
testcase_21 AC 211 ms
28,272 KB
testcase_22 AC 208 ms
28,804 KB
testcase_23 AC 215 ms
27,196 KB
testcase_24 AC 209 ms
28,364 KB
testcase_25 AC 208 ms
29,136 KB
testcase_26 AC 302 ms
31,212 KB
testcase_27 AC 292 ms
34,116 KB
testcase_28 AC 307 ms
32,184 KB
testcase_29 AC 311 ms
33,748 KB
testcase_30 AC 304 ms
31,888 KB
testcase_31 AC 303 ms
32,180 KB
testcase_32 AC 319 ms
32,740 KB
testcase_33 AC 104 ms
21,532 KB
testcase_34 AC 100 ms
21,540 KB
testcase_35 AC 103 ms
21,548 KB
testcase_36 AC 138 ms
28,456 KB
testcase_37 AC 129 ms
26,496 KB
testcase_38 AC 106 ms
24,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,M;
ll W[202020];
vector<pair<int,ll>> E[202020];
ll dp[202020][2];


template<typename V> struct ConvexHull {
	deque<pair<V,V>> Q;
	V calc(pair<V,V> p, V x) {
		return p.first*x+p.second;
	}
	int dodo(pair<V,V> A,pair<V,V> B, pair<V,V> C) { // max or min
		return ((__int128)(A.second-C.second)*(B.first-A.first)<=(__int128)(A.second-B.second)*(C.first-A.first));
	}

	void add(V a, V b) { // add ax+b
		Q.push_back({a,b});
		int v;
		while((v=Q.size())>=3 && dodo(Q[v-3],Q[v-2],Q[v-1]))
			Q[v-2]=Q[v-1], Q.pop_back();
	}
	void add(vector<pair<V,V>> v) {
		sort(ALL(v)); 
		reverse(ALL(v)); //傾きの大きい順
		FORR2(a,b,v) add(a,b);
		
	}
	V query(V x) {
		int L=-1,R=Q.size()-1;
		while(R-L>1) {
			int M=(L+R)/2;
			(((calc(Q[M],x)>=calc(Q[M+1],x)))?L:R)=M;
		}
		return calc(Q[R],x);
	}
};
ConvexHull<ll> ch;


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	FOR(i,N) cin>>W[i];
	FOR(i,M) {
		ll v;
		cin>>x>>y>>v;
		x--,y--;
		E[x].push_back({y,v});
		E[y].push_back({x,v});
	}
	FOR(i,N) dp[i][0]=dp[i][1]=1LL<<60;
	dp[0][0]=dp[N-1][1]=0;
	priority_queue<pair<ll,int>> Q;
	Q.push({0,0});
	Q.push({0,2*N-1});
	while(Q.size()) {
		ll co=-Q.top().first;
		int cur=Q.top().second%N;
		int id=Q.top().second/N;
		Q.pop();
		if(dp[cur][id]!=co) continue;
		FORR2(e,c,E[cur]) if(chmin(dp[e][id],co+c)) Q.push({-dp[e][id],id*N+e});
	}
	
	vector<pair<ll,ll>> V;
	FOR(i,N) V.push_back({W[i],dp[i][1]});
	ch.add(V);
	ll ret=dp[N-1][0];
	FOR(i,N) chmin(ret,dp[i][0]+ch.query(W[i]));
	cout<<ret<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0