#include <bits/stdc++.h>
using namespace std;
#define MOEZ ios_base::sync_with_stdio(false);cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define F first
#define S second
#define pb push_back
#define all(v) v.begin() , v.end()
#define eb emplace_back
#define getunique(v) {sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end());}
#define mem(d , x) memset(d , x , sizeof(d));
#define sz(x) (int)x.size()
const int N = 2e5+5;
//const ll M = 1e9+7;
//const ll inf = 1e18;
//const ld eps = 1e-6;
int n , id[N] , p[N] , m;
vector<pair<int , int>>adj[N];
int d[N];
int main(){
MOEZ
cin >> n >> m;
for(int i = 0; i < n;i++){
d[i] = 1e8;
}
for(int i = 0; i < m;i++){
cin >> id[i];
cin >> p[i];
int cnt = 1;
for(int j = id[i]+p[i]; j < n;j += p[i]){
adj[id[i]].pb({j , cnt++});
}
cnt = 1;
for(int j = id[i]-p[i]; j >= 0;j -= p[i]){
adj[id[i]].pb({j , cnt++});
}
}
priority_queue<pair<int , int> , vector<pair<int , int>> , greater<>>q;
q.push({id[0] , 0});
while(!q.empty()){
int u = q.top().F , cost = q.top().S;
q.pop();
if(d[u] < cost)continue;
if(u == id[1])break;
for(auto x:adj[u]){
int v = x.F , nc = cost+x.S;
if(nc < d[v]){
d[v] = nc;
q.push({v , nc});
}
}
}
cout << (d[id[1]] == 1e8 ? -1 : d[id[1]]);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
4 ms |
4948 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |