Submission #63823

#TimeUsernameProblemLanguageResultExecution timeMemory
63823kingpig9Jakarta Skyscrapers (APIO15_skyscraper)C++11
100 / 100
171 ms16616 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 30010;

#define debug(...) fprintf(stderr, __VA_ARGS__)
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define fillchar(a, s) memset((a), (s), sizeof(a))

int N, M;
int S, T;
vector<int> jump[MAXN];
int dist[MAXN];
bool vis[MAXN];

int main() {
	scanf("%d %d", &N, &M);
	for (int i = 0; i < M; i++) {
		int x, p;
		scanf("%d %d", &x, &p);
		jump[x].push_back(p);
		if (i == 0) {
			S = x;
		} else if (i == 1) {
			T = x;
		}
	}

	for (int i = 0; i < N; i++) {
		sort(all(jump[i]));
		jump[i].resize(unique(all(jump[i])) - jump[i].begin());
	}

	for (int i = 0; i < N; i++) {
		dist[i] = INT_MAX / 2;
	}
	dist[S] = 0;

	priority_queue<pii, vector<pii>, greater<pii>> pq;
	pq.push({0, S});
	while (!pq.empty()) {
		int w = pq.top().fi;
		int u = pq.top().se;
		pq.pop();

		if (u == T) {
			printf("%d\n", w);
			return 0;
		}

		if (vis[u]) {
			continue;
		}
		vis[u] = true;

		for (int p : jump[u]) {
			for (int i = 1; u + i * p < N; i++) {
				int v = u + i * p;
				int nw = w + i;
				if (dist[v] > nw) {
					dist[v] = nw;
					pq.push({nw, v});
				}
				if (binary_search(all(jump[v]), p)) {
					break;
				}
			}

			for (int i = 1; u - i * p >= 0; i++) {
				int v = u - i * p;
				int nw = w + i;
				if (dist[v] > nw) {
					dist[v] = nw;
					pq.push({nw, v});
				}
				if (binary_search(all(jump[v]), p)) {
					break;
				}
			}
		}
	}
	puts("-1");
}

Compilation message (stderr)

skyscraper.cpp: In function 'int main()':
skyscraper.cpp:23:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &N, &M);
  ~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:26:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &x, &p);
   ~~~~~^~~~~~~~~~~~~~~~~
#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...