#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define fs first
#define sc second
#define pb push_back
void solve() {
int n,m;
cin>>n>>m;
pii a[m];
vector<pii> adj[n*m+n];
for(int i=0;i<m;i++){
int b,p;
cin>>b>>p;
a[i]={b,p};
for(int x=b%p;x<n;x++){
adj[n*i+x].pb({n*m+x,0});
}
adj[n*m+b].pb({n*i+b,0});
for(int x=b%p;x+p<n;x++){
adj[n*i+x].pb({n*i+x+p,1});
adj[n*i+x+p].pb({n*i+x,1});
}
}
pii dist[n*m+n];
bool vis[n*m+n];
for(int i=0;i<n*m+n;i++){
dist[i].fs=1e9;
vis[i]=0;
}
dist[a[0].fs]={0,a[0].fs};
priority_queue<pii> pq;
pq.push({0,a[0].fs});
while(!pq.empty()){
int v=pq.top().sc;
pq.pop();
if(vis[v])continue;
vis[v]=1;
for(auto[u,w]:adj[v])if(!vis[u]){
dist[u]=min(dist[u],{dist[v].fs+w,v});
pq.push({-dist[u].fs,u});
}
}
int ans=dist[n+a[1].fs].fs;
if(ans==1e9)ans=-1;
cout<<ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
452 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
17 ms |
12200 KB |
Output is correct |
12 |
Correct |
37 ms |
16220 KB |
Output is correct |
13 |
Correct |
36 ms |
16336 KB |
Output is correct |
14 |
Correct |
14 ms |
12124 KB |
Output is correct |
15 |
Correct |
15 ms |
12124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
16 ms |
12380 KB |
Output is correct |
12 |
Correct |
40 ms |
16208 KB |
Output is correct |
13 |
Correct |
35 ms |
16276 KB |
Output is correct |
14 |
Correct |
15 ms |
12124 KB |
Output is correct |
15 |
Correct |
13 ms |
12124 KB |
Output is correct |
16 |
Correct |
11 ms |
10500 KB |
Output is correct |
17 |
Correct |
119 ms |
85520 KB |
Output is correct |
18 |
Correct |
137 ms |
101352 KB |
Output is correct |
19 |
Correct |
73 ms |
67412 KB |
Output is correct |
20 |
Runtime error |
412 ms |
262144 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
452 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
452 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
15 ms |
12428 KB |
Output is correct |
12 |
Correct |
42 ms |
16220 KB |
Output is correct |
13 |
Correct |
38 ms |
16216 KB |
Output is correct |
14 |
Correct |
16 ms |
12124 KB |
Output is correct |
15 |
Correct |
14 ms |
12164 KB |
Output is correct |
16 |
Correct |
11 ms |
10588 KB |
Output is correct |
17 |
Correct |
104 ms |
85480 KB |
Output is correct |
18 |
Correct |
111 ms |
101200 KB |
Output is correct |
19 |
Correct |
74 ms |
67416 KB |
Output is correct |
20 |
Runtime error |
387 ms |
262144 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
4 ms |
2652 KB |
Output is correct |
11 |
Correct |
16 ms |
12408 KB |
Output is correct |
12 |
Correct |
37 ms |
16216 KB |
Output is correct |
13 |
Correct |
40 ms |
16216 KB |
Output is correct |
14 |
Correct |
15 ms |
12120 KB |
Output is correct |
15 |
Correct |
14 ms |
12208 KB |
Output is correct |
16 |
Correct |
12 ms |
10588 KB |
Output is correct |
17 |
Correct |
112 ms |
85532 KB |
Output is correct |
18 |
Correct |
121 ms |
101200 KB |
Output is correct |
19 |
Correct |
73 ms |
67580 KB |
Output is correct |
20 |
Runtime error |
386 ms |
262144 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |