Submission #39456

#TimeUsernameProblemLanguageResultExecution timeMemory
39456qoo2p5Jakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
1000 ms5428 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int INF = (int) 1e9 + 123;
const ll LINF = (ll) 1e18 + 123;

#define rep(i, s, t) for (auto i = (s); i < (t); ++(i))
#define per(i, s, t) for (auto i = (s); i >= (t); --(i))
#define sz(x) ((int) (x).size())
#define mp make_pair
#define pb push_back

bool mini(auto &x, const auto &y) {
	if (y < x) {
		x = y;
		return 1;
	}
	return 0;
}

bool maxi(auto &x, const auto &y) {
	if (y < x) {
		x = y;
		return 1;
	}
	return 0;
}

void run();

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	run();
	return 0;
}

const int N = 30003;

int n, m;
int b[N], p[N];
int dist[N];
bool used[N];
vector<int> which[N];

void run() {
	cin >> n >> m;
	rep(i, 0, m) {
		cin >> b[i] >> p[i];
		which[b[i]].pb(p[i]);
	}
	
	fill(dist, dist + n, INF);
	dist[b[0]] = 0;
	priority_queue<pair<int, int>> q;
	q.push({0, b[0]});
	while (sz(q)) {
		int v = q.top().second;
		q.pop();
		if (used[v]) continue;
		used[v] = 1;
		for (int step : which[v]) {
			rep(i, 1, N) {
				int to = v + step * i;
				if (to >= n) break;
				if (mini(dist[to], dist[v] + i)) {
					q.push({-dist[to], to});
				}
			}
			rep(i, 1, N) {
				int to = v - step * i;
				if (to < 0) break;
				if (mini(dist[to], dist[v] + i)) {
					q.push({-dist[to], to});
				}
			}
		}
	}
	
	cout << (dist[b[1]] == INF ? -1 : dist[b[1]]) << "\n";
}
#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...