Submission #436844

#TimeUsernameProblemLanguageResultExecution timeMemory
436844KarliverJakarta Skyscrapers (APIO15_skyscraper)C++14
0 / 100
1 ms204 KiB
 
	
#include <bits/stdc++.h>

#define FIXED_FLOAT(x)  std::fixed <<std::setprecision(20) << (x)
#define all(v) (v).begin(), (v).end()
using namespace  std;
#define forn(i,n) for (int i = 0; i < (n); ++i)
#define rforn(i, n) for(int i = (n) - 1;i >= 0;--i)
using ll = long long;
int mod = (ll)1e9 + 7;
#define PI acos(-1)
typedef pair<int, int> pairs;

const int INF = 1e9 + 1;
const int N = 2e5 + 100;
const double eps = 1e-7;

template <class T> using V = vector<T>;  
template <class T> using VV = V<V<T>>;  

template <typename XPAX>
void ckma(XPAX &x, XPAX y) {
    x = (x < y ? y : x);
}
template <typename XPAX>
void ckmi(XPAX &x, XPAX y) {
    x = (x > y ? y : x);
}

void solve() {


	int n, m;
	cin >> n >> m;

	VV<pairs> g(n);

	V<int> f(n), p(n);
	forn(i, m) cin >> f[i] >> p[i];
	int x, des;
	forn(i, m) {
		x = f[i];
		des = p[i];
		int cnt = 1;
		for(int j = x + des;j < n;j += des) {
			g[x].push_back({j, cnt});
			//g[j].push_back({x, cnt});
			++cnt;
		}
		cnt = 1;
		for(int j = x - des;j >= 0;j -= des) {
			g[x].push_back({j, cnt});
			//g[j].push_back({x, cnt});
			++cnt;
		}
	}

	V<int> dis(n, INF);

	priority_queue<pairs> q;

	q.push({0, f[0]});

	while(q.size()) {
		x = q.top().second;
		int s = q.top().first;
		s = -s;
		q.pop();
		for(auto c : g[x]) {
			if(s + c.second < dis[c.first]) {
				q.push({-(s + c.second), c.first});
				dis[c.first] = s + c.second;
				if(c.first == f[1]) {
					cout << dis[f[1]] << '\n';
					exit(0);
				}
			}
		}
	}
	cout << -1 << '\n';






}
void test_case() {
    int t;
    cin >> t;
    forn(p, t) {

        //cout << "Case #" << p + 1 << ": ";
        solve();
    }
}
int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);

    solve();

}
  
#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...