Submission #389656

#TimeUsernameProblemLanguageResultExecution timeMemory
389656milleniumEeeeJakarta Skyscrapers (APIO15_skyscraper)C++17
57 / 100
1087 ms8104 KiB
#include <bits/stdc++.h>
#define fr first
#define sc second
#define pii pair<int, int>
#define pb push_back
#define szof(s) (int)s.size()
#define all(s) s.begin(), s.end()
#define fastInp ios_base::sync_with_stdio(0); cin.tie(0);
//#define int long long
using namespace std;

const int MAXN = (int)2e5 + 5;
const int INF = 1e18;

int pos[MAXN], p[MAXN];
int n, m;

int dist[MAXN];
vector <int> edge[MAXN];

signed main() {
	fastInp;
	cin >> n >> m;
	for (int i = 0; i < m; i++) {
		cin >> pos[i] >> p[i];
		edge[pos[i]].pb(p[i]);
	}
	memset(dist, -1, sizeof(dist));
	dist[pos[0]] = 0;
	priority_queue <pii> pq;
	pq.push({0, pos[0]});
	while (!pq.empty()) {
		int v = pq.top().sc;
		int cost = -pq.top().fr;
		pq.pop();
		if (cost > dist[v]) {
			continue;
		}
		for (auto x : edge[v]) {
			if (x == 0) {
				continue;
			}
			for (int to = v % x; to < n; to += x) {
				int c = abs(v - to) / x;
				if (dist[to] == -1 || dist[to] > dist[v] + c) {
					dist[to] = dist[v] + c;
					pq.push({-dist[to], to});
				}
			}
		}
	}
	cout << dist[pos[1]];
}

Compilation message (stderr)

skyscraper.cpp:13:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
   13 | const int INF = 1e18;
      |                 ^~~~
#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...