#include<bits/stdc++.h>
using namespace std;
set<int>doge[30100];
int dist[30100];
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;
int main(){
memset(dist,9,sizeof dist);
int n,m;
cin>>n>>m;
int a,b;cin>>a>>b;
doge[a].insert(b);
dist[a]=0;
pq.push({0,a});
for(int i=1;i<m;i++){
cin>>a>>b;
doge[a].insert(b);
}
while(pq.size()){
auto[a,b]=pq.top();
pq.pop();
if(dist[b]!=a)continue;
for(auto d:doge[b]){
for(int i=b-d;i>=0;i-=d){
int D=a+(b-i)/d;
if(dist[i]>D&&doge[i].size())
pq.push({dist[i]=D,i});
if(doge[i].count(d))
break;
}
for(int i=b+d;i<n;i+=d){
int D=a+(i-b)/d;
if(dist[i]>D&&doge[i].size())
pq.push({dist[i]=D,i});
if(doge[i].count(d))
break;
}
}
}
cout<<(dist[1]!=dist[30099]?dist[1]:-1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2136 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1884 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1884 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |