Submission #39454

#TimeUsernameProblemLanguageResultExecution timeMemory
39454qoo2p5Jakarta Skyscrapers (APIO15_skyscraper)C++14
36 / 100
213 ms262144 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<pair<int, int>> g[N];

void run() {
	cin >> n >> m;
	rep(i, 0, m) {
		cin >> b[i] >> p[i];
	}
	
	rep(i, 0, m) {
		rep(j, 0, N) {
			int to = b[i] + (j + 1) * p[i];
			if (to >= n) {
				break;
			}
			g[b[i]].pb({to, j + 1});
		}
		rep(j, 0, N) {
			int to = b[i] - (j + 1) * p[i];
			if (to < 0) {
				break;
			}
			g[b[i]].pb({to, j + 1});
		}
	}
	
	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 (auto &e : g[v]) {
			int u = e.first;
			if (mini(dist[u], dist[v] + e.second)) {
				q.push({-dist[u], u});
			}
		}
	}
	
	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...