Submission #766354

#TimeUsernameProblemLanguageResultExecution timeMemory
766354TobJakarta Skyscrapers (APIO15_skyscraper)C++14
57 / 100
426 ms96768 KiB
#include <bits/stdc++.h>

#define ll long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

using namespace std;

typedef pair <ll, ll> pii;

const int N = 3e4 + 7, SN = 2e3 + 7;
ll inf = 1e18;

int n, m;
ll a[N], b[N], dis[N], ed[SN][SN];
vector <pii> v[SN];

int main () {
	cin >> n >> m;
	
	for (int i = 0; i < n; i++) dis[i] = inf;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) ed[i][j] = inf;
	}
	for (int i = 0; i < m; i++) {
		cin >> a[i] >> b[i];
		for (int j = a[i]%b[i]; j < n; j += b[i]) ed[a[i]][j] = min(ed[a[i]][j], abs(a[i] - j) / b[i]);
	}
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (ed[i][j]) v[i].pb({j, ed[i][j]});
		}
	}
	
	set <pii> s;
	s.insert({0, a[0]});
	dis[a[0]] = 0;
	while (!s.empty()) {
		auto p = s.begin();
		ll x = (*p).S;
		s.erase(p);
		for (auto it : v[x]) {
			ll y = it.F, w = it.S;
			if (dis[x] + w < dis[y]) {
				if (dis[y] != inf) s.erase({dis[y], y});
				dis[y] = dis[x] + w;
				s.insert({dis[y], y});
			}
		}
	}
	
	//for (int i = 0; i < n; i++) cout << dis[i] << " ";
	
	if (dis[a[1]] != inf) cout << dis[a[1]];
	else cout << -1;

	return 0;
}
#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...