Submission #341801

# Submission time Handle Problem Language Result Execution time Memory
341801 2020-12-31T04:51:41 Z egod1537 Jakarta Skyscrapers (APIO15_skyscraper) C++14
0 / 100
1 ms 1260 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pi;
#define to first
#define cost second
#define pos first
#define range second

struct Q {
	ll pos, cost;
};
struct cmp {
	bool operator()(const Q& a, const Q& b) {
		return a.cost > b.cost;
	}
};

vector<pi> V[30001];
ll dst[30001];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	fill(dst, dst + 30001, LLONG_MAX);

	int n, m; cin >> n >> m;
	vector<pi> doge(m);
	vector<int> posdoge(n + 1, -1);

	int s = 0, e = 0;
	for (int i = 0; i < m; i++) {
		cin >> doge[i].pos >> doge[i].range;
		if (i == 0) s = doge[i].pos;
		if (i == 1) e = doge[i].pos;
	}
	sort(doge.begin(), doge.end());

	for (int i = 0; i < m; i++)
		if (doge[i].pos == s) { s = i;  break; }
	for (int i = 0; i < m; i++)
		if (doge[i].pos == e) { e = i;  break; }

	for (int i = 0; i < m; i++)
		posdoge[doge[i].pos] = i;

	for (int i = 0; i < m; i++) {
		pi& now = doge[i];
		for (int j = now.pos + now.range; j < n; j += now.range) {
			int idx = posdoge[j];
			if (idx == -1) continue;

			ll dst = doge[idx].pos - now.pos;
			V[i].push_back({ idx, dst / now.range });
			if (now.range == doge[idx].range) break;
		}
		for (int j = now.pos - now.range; j >= 0; j -= now.range) {
			int idx = posdoge[j];
			if (idx == -1) continue;

			ll dst = now.pos - doge[idx].pos;
			V[i].push_back({ idx, dst / now.range });
			if (now.range == doge[idx].range) break;
		}
	}

	priority_queue<Q, vector<Q>, cmp> pq;
	pq.push({ s, 0 });
	while (!pq.empty()) {
		Q top = pq.top(); pq.pop();

		for (pi w : V[top.pos]) {
			if (dst[w.to] > top.cost + w.cost) {
				dst[w.to] = top.cost + w.cost;
				pq.push({ w.to, dst[w.to] });
			}
		}
	}

	if (dst[e] == LLONG_MAX) cout << -1;
	else cout << dst[e];

	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1260 KB Output is correct
2 Incorrect 1 ms 1260 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1260 KB Output is correct
2 Incorrect 1 ms 1260 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1260 KB Output is correct
2 Incorrect 1 ms 1260 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1260 KB Output is correct
2 Incorrect 1 ms 1260 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1260 KB Output is correct
2 Incorrect 1 ms 1260 KB Output isn't correct
3 Halted 0 ms 0 KB -