제출 #1329762

#제출 시각아이디문제언어결과실행 시간메모리
1329762tkm_algorithmsJakarta Skyscrapers (APIO15_skyscraper)C++20
57 / 100
1096 ms4716 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
//#define int ll
#define rep(x,s,e) for (auto x=(s)-((s)>(e));x!=(e)-((s)>(e));((s)<(e)?x++:x--))
using P = pair<int, int>;
using tp = tuple<int, int, int>;
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
const char nl = '\n';
const int mod = 998244353;
const int inf = 1e9;
//const int NM

void solve() {
	int n, m; cin >> n >> m;
	int S = -1, E = -1;
	set<P> s;
	rep(i, 0, m) {
		int x, y; cin >> x >> y;
		if (i == 0)S = x;
		if (i == 1)E = x;
		s.insert(P(x, y));
	}
	
	m = sz(s);
	vector<int> b(m), p(m), dist(n, inf);
	auto it = s.begin();
	rep(i, 0, m)
		b[i] = it->first,
		p[i] = it->second,
		it++;
	
	vector<P> light[n], heavy[n];
	rep(i, 0, m)
		 {
			if(b[i]+p[i] < n)light[b[i]].push_back(P(b[i]+p[i], p[i]));
			if (0 <= b[i]-p[i])light[b[i]].push_back(P(b[i]-p[i], -p[i]));
		}
		
	priority_queue<tp, vector<tp>, greater<tp>> pq;
	pq.push({0, S, 0}); dist[S] = 0;
	while(!pq.empty()) {
		auto [cost, nd, pw] = pq.top(); pq.pop();
		if (pw == 0 && dist[nd] != cost)continue;
		//cout << nd << " " << cost << " " << pw << nl;
		if (pw == 0) { // can use any edge
			for (auto [ch, w]: light[nd]) {
				pq.push({cost+1, ch, w});
				if (cost+1 < dist[ch])
					dist[ch] = cost+1,
					pq.push({cost+1, ch, 0});
			}
		} else { // can continue using light edge or stop
			if (0 <= nd+pw && nd+pw < n) {
				//if(cost+1 < dist[nd+pw])
					pq.push({cost+1, nd+pw, pw});
				if(cost+1<dist[nd+pw])pq.push({cost+1, nd+pw, 0}),
				dist[nd+pw] = min(dist[nd+pw], cost+1);
			}
		}
	}
	
	if (dist[E] == inf)dist[E] = -1;
	cout << dist[E] << nl;
}

int32_t main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    solve();
    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...
#Verdict Execution timeMemoryGrader output
Fetching results...