#include <bits/stdc++.h>
#define hash _hash_
#define left _left_
#define y1 _y1_
using namespace std;
using ll = long long;
const ll oo = 1e18;
/*----------- I alone decide my fate! ----------*/
/* Chiec den ong sao, sao 5 canh tuoi mau */
int N, M;
int B[30009], P[30009];
vector<pair<int,int>> adj[30009];
ll dista[30009];
void Dijkstra() {
for (int i = 0; i < M; i++) dista[i] = oo;
priority_queue<pair<ll,int>, vector<pair<ll,int>>, greater<pair<ll,int>>> pq;
dista[0] = 0;
pq.push({0, 0});
while (!pq.empty()) {
auto [cost, u] = pq.top();
pq.pop();
if (cost != dista[u]) continue;
if (u == 1) {
cout << cost;
return;
}
for (auto [v, w] : adj[u]) {
if (dista[v] > cost + w) {
dista[v] = cost + w;
pq.push({dista[v], v});
}
}
}
cout << -1;
}
void solve() {
cin >> N >> M;
for (int i = 0; i < M; i++) {
cin >> B[i] >> P[i];
}
for (int i = 0; i < M; i++) {
for (int j = i + 1; j < M; j++) {
if (abs(B[i] - B[j]) % P[i] == 0) {
ll w = abs(B[i] - B[j]) / P[i];
adj[i].push_back({j, w});
}
if (abs(B[i] - B[j]) % P[j] == 0) {
ll w = abs(B[i] - B[j]) / P[j];
adj[j].push_back({i, w});
}
}
}
Dijkstra();
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
solve();
return 0;
}
/*
How can you see into my eyes, like open doors?
Leading you down into my core, where I've become so numb
Without a soul, my spirit's sleeping somewhere cold
Until you find it here and bring it back home!
Wake me up! Wake me up inside
Cant wake up? Wake me up inside
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |