Submission #455359

#TimeUsernameProblemLanguageResultExecution timeMemory
455359Hamed5001Jakarta Skyscrapers (APIO15_skyscraper)C++14
100 / 100
997 ms3776 KiB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

const int mxN = 3e4, OO = 0x3f3f3f3f;
int N, M;
vector<int> doges[mxN];

int dijkstra(int st, int en) {
	vector<int> dist(N, OO);
	priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
	pq.push({0, st});

	while(pq.size()) {
		auto node = pq.top();

		pq.pop();
		if (node.first > dist[node.second])
			continue;

		dist[node.second] = node.first;

		if (node.second == en)
			return node.first;

		for (auto doge : doges[node.second]) {
			for (int i = node.second+doge, j = 1; i < N; i+=doge, j++) {
				if (dist[i] > node.first + j) {
					dist[i] = node.first + j;
					pq.push({dist[i], i});
				}
			}
			for (int i = node.second-doge, j = 1; i >= 0; i-=doge, j++) {
				if (dist[i] > node.first + j) {
					dist[i] = node.first + j;
					pq.push({dist[i], i});
				}
			}
		}
	}
	return -1;
}	

void solve() {
	cin >> N >> M;

	int stB, enB;
	for (int i = 0; i < M; i++) {
		int B, P;
		cin >> B >> P;
		if (!i) stB = B;
		if (!(i-1)) enB = B;
		doges[B].push_back(P);
	}


	cout << dijkstra(stB, enB);
}

int main() {

	
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	solve();
}

Compilation message (stderr)

skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:58:27: warning: 'enB' may be used uninitialized in this function [-Wmaybe-uninitialized]
   58 |  cout << dijkstra(stB, enB);
      |                           ^
skyscraper.cpp:58:27: warning: 'stB' may be used uninitialized in this function [-Wmaybe-uninitialized]
#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...