結果

問題 No.2477 Drifting
ユーザー shobonvipshobonvip
提出日時 2023-09-18 21:03:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 709 ms / 2,000 ms
コード長 2,896 bytes
コンパイル時間 6,632 ms
コンパイル使用メモリ 289,516 KB
実行使用メモリ 40,704 KB
最終ジャッジ日時 2023-09-18 21:03:42
合計ジャッジ時間 15,160 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 592 ms
40,580 KB
testcase_01 AC 374 ms
30,584 KB
testcase_02 AC 602 ms
40,552 KB
testcase_03 AC 500 ms
27,928 KB
testcase_04 AC 182 ms
19,256 KB
testcase_05 AC 438 ms
25,840 KB
testcase_06 AC 709 ms
40,704 KB
testcase_07 AC 681 ms
40,656 KB
testcase_08 AC 495 ms
33,756 KB
testcase_09 AC 209 ms
23,336 KB
testcase_10 AC 228 ms
25,904 KB
testcase_11 AC 221 ms
22,024 KB
testcase_12 AC 304 ms
29,292 KB
testcase_13 AC 5 ms
11,396 KB
testcase_14 AC 5 ms
11,104 KB
testcase_15 AC 6 ms
11,096 KB
testcase_16 AC 201 ms
20,848 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;
}

ll floor_sqrt(ll n){
	ll ub = 3e9 + 30;
	ll lb = 0;
	while(ub - lb > 1){
		ll t = (ub + lb) / 2;
		if (t * t <= n){
			lb = t;
		}else{
			ub = t;
		}
	}
	return lb;
}


//defmodfact
const int COMinitMAX = 992844;
mint fact[COMinitMAX+1], factinv[COMinitMAX+1];

void modfact(){
	fact[0] = 1;
	for (int i=1; i<=COMinitMAX; i++){
		fact[i] = fact[i-1] * i;
	}
	factinv[COMinitMAX] = fact[COMinitMAX].inv();
	for (int i=COMinitMAX-1; i>=0; i--){
		factinv[i] = factinv[i+1] * (i+1);
	}
}

mint cmb(int a, int b){
	if (a<b || b<0) return mint(0);
	return fact[a]*factinv[b]*factinv[a-b];
}
//--------

int main(){
	int n, m; cin >> n >> m;
	vector ikeru(n, vector<pair<int,ll>>(0));
	rep(i,0,m){
		int u, v; ll w; cin >> u >> v >> w;
		u--; v--;
		ikeru[u].push_back(pair(v, w));
	}
	int k; cin >> k;
	vector<int> a(k), b(k), c(k);
	map<pair<int,int>,int> mp;
	vector bs(n, vector<int>(0));
	set<tuple<int,int,int>> dame;
	rep(i,0,k){
		cin >> a[i] >> b[i] >> c[i];
		a[i]--; b[i]--; c[i]--;
		if (mp.find(pair(a[i], b[i])) == mp.end()){
			mp[pair(a[i], b[i])] = (int)mp.size();
			bs[b[i]].push_back(a[i]);
		}
		dame.insert(make_tuple(a[i], b[i], c[i]));
	}
	int l = mp.size();
	vector<ll> d(n,1e18);
	vector<ll> dk(l,1e18);
	d[0] = 0;
	rep(i,0,n){
		vector<pair<ll,int>> ds;
		ds.push_back(pair(d[i], -1));
		map<int,vector<int>> am;
		rep(j,0,(int)bs[i].size()){
			ds.push_back(pair(dk[mp[pair(bs[i][j], i)]], bs[i][j]));
		}
		sort(ds.begin(), ds.end());
		for(auto[j,w]:ikeru[i]){
			rep(x,0,(int)ds.size()){
				if (ds[x].second == -1 || dame.find(make_tuple(ds[x].second, i, j)) == dame.end()){
					if(mp.find(pair(i, j))==mp.end()){
						chmin(d[j], w+ds[x].first);
					}else{
						chmin(dk[mp[pair(i, j)]], w+ds[x].first);
					}
					break;
				}
			}
		}
	}
	if (d[n-1] == 1e18){
		cout << -1 << '\n';
	}else{
		cout << d[n-1] << '\n';
	}

}

0