제출 #1314275

#제출 시각아이디문제언어결과실행 시간메모리
1314275vlomaczk치료 계획 (JOI20_treatment)C++20
0 / 100
25 ms4332 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	ll n, m;
	cin >> n >> m;
	vector<ll> T(m), L(m), R(m), C(m);
	for(ll i=0; i<m; ++i) {
		cin >> T[i] >> L[i] >> R[i] >> C[i];
	}
	vector<ll> targets;
	ll inf = 1e18;
	vector<ll> dist(m, inf);
	auto check = [&](ll i, ll j) {
		if(T[i] >= T[j]) return (L[j] - T[j] <= R[i] - T[i]);
		return (L[j] + T[j] <= R[i] + T[i]);
	};
	for(ll i=0; i<m; ++i) {
		if(L[i]==1) dist[i] = C[i];
		if(R[i]==n) targets.push_back(i);
	}
	priority_queue<pair<ll, ll>> pq;
	for(ll i=0; i<m; ++i) if(dist[i] < inf) pq.push({-dist[i], i});
	while(pq.size()) {
		auto[dv, v] = pq.top();
		pq.pop(); dv*=-1;
		if(dv!=dist[v]) continue;
		for(ll u=0; u<m; ++u) {
			if(!check(v,u)) continue;
			if(dist[u] > dist[v] + C[u]) {
				dist[u] = dist[v] + C[u];
				pq.push({-dist[u], u});
			}
		}
	}
	ll ans = inf;
	for(ll x : targets) {
		ans = min(ans, dist[x]);
	}
	if(ans==inf) cout << "-1\n";
	else cout << ans << "\n";

	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...