結果

問題 No.2764 Warp Drive Spacecraft
ユーザー 沙耶花沙耶花
提出日時 2024-05-17 22:29:22
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 580 ms / 3,000 ms
コード長 2,547 bytes
コンパイル時間 4,871 ms
コンパイル使用メモリ 275,464 KB
実行使用メモリ 26,168 KB
最終ジャッジ日時 2024-05-18 00:10:41
合計ジャッジ時間 15,920 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 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,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 280 ms
22,072 KB
testcase_17 AC 281 ms
22,096 KB
testcase_18 AC 274 ms
22,076 KB
testcase_19 AC 435 ms
20,168 KB
testcase_20 AC 444 ms
21,316 KB
testcase_21 AC 436 ms
20,252 KB
testcase_22 AC 438 ms
21,252 KB
testcase_23 AC 436 ms
20,192 KB
testcase_24 AC 438 ms
21,456 KB
testcase_25 AC 436 ms
21,408 KB
testcase_26 AC 557 ms
25,332 KB
testcase_27 AC 553 ms
25,208 KB
testcase_28 AC 575 ms
25,616 KB
testcase_29 AC 564 ms
25,688 KB
testcase_30 AC 550 ms
25,196 KB
testcase_31 AC 569 ms
26,168 KB
testcase_32 AC 580 ms
26,024 KB
testcase_33 AC 275 ms
15,316 KB
testcase_34 AC 275 ms
15,328 KB
testcase_35 AC 273 ms
15,472 KB
testcase_36 AC 262 ms
22,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000001
/**
 * Author: Simon Lindholm
 * Date: 2017-04-20
 * License: CC0
 * Source: own work
 * Description: Container where you can add lines of the form kx+m, and query maximum values at points x.
 *  Useful for dynamic programming (``convex hull trick'').
 * Time: O(\log N)
 * Status: stress-tested
 */
using ll =long long;
struct Line {
	mutable ll k, m, p;
	bool operator<(const Line& o) const { return k < o.k; }
	bool operator<(ll x) const { return p < x; }
};

struct LineContainer : multiset<Line, less<>> {
	// (for doubles, use inf = 1/.0, div(a,b) = a/b)
	static const ll inf = LLONG_MAX - 1000000000LL;
	ll div(ll a, ll b) { // floored division
		return a / b - ((a ^ b) < 0 && a % b); }
	bool isect(iterator x, iterator y) {
		if (y == end()) return x->p = inf, 0;
		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	void add(ll k, ll m) {
		auto z = insert({k, m, 0}), y = z++, x = y;
		while (isect(y, z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
		while ((y = x) != begin() && (--x)->p >= y->p)
			isect(x, erase(y));
	}
	ll query(ll x) {
		assert(!empty());
		auto l = *lower_bound(x);
		return l.k * x + l.m;
	}
};

vector<long long> get_distance(vector<vector<pair<int,long long>>> &E,int s){
	int N = E.size();
	vector<long long> dist(N,Inf64);
	dist[s] = 0;
	priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> pq;
	pq.push({0,s});
	while(!pq.empty()){
		auto [d,u] = pq.top();
		pq.pop();
		if(dist[u] !=d )continue;
		for(auto [v,w]:E[u]){
			if(dist[v] > dist[u] + w){
				dist[v] = dist[u] + w;
				pq.push({dist[v],v});
			}
		}
	}
	return dist;
}

int main(){
	int N,M;
	cin>>N>>M;
	vector<long long> w(N);
	rep(i,N)cin>>w[i];
	
	vector<vector<pair<int,long long>>> E(N);
	rep(i,M){
		int u,v;
		long long ww;
		cin>>u>>v>>ww;
		u--,v--;
		E[u].emplace_back(v,ww);
		E[v].emplace_back(u,ww);
		//E[v].push_back({u,ww});
	}
	auto x = get_distance(E,0),y = get_distance(E,N-1);
	long long ans = x[N-1];
	
	
	LineContainer cht;
	rep(i,N){
		if(x[i]==Inf64)continue;
		cht.add(-w[i],-x[i]);
	}
	if(!cht.empty()){
	rep(i,N){
		if(y[i]==Inf64)continue;
		ans = min(ans,-cht.query(w[i]) + y[i]);
	}
	}
	cout<<ans<<endl;
	
	return 0;
}
0