#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});
}
}
int dist[n*m+n];
bool vis[n*m+n];
for(int i=0;i<n*m+n;i++){
dist[i]=1e9;
vis[i]=0;
}
dist[a[0].fs]=0;
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]+w);
pq.push({-dist[u],u});
}
}
int ans=dist[n+a[1].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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
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 |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2652 KB |
Output is correct |
11 |
Correct |
15 ms |
11612 KB |
Output is correct |
12 |
Correct |
36 ms |
15516 KB |
Output is correct |
13 |
Correct |
39 ms |
15452 KB |
Output is correct |
14 |
Correct |
18 ms |
11352 KB |
Output is correct |
15 |
Correct |
14 ms |
11356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
600 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 |
15 ms |
11552 KB |
Output is correct |
12 |
Correct |
38 ms |
15452 KB |
Output is correct |
13 |
Correct |
40 ms |
15448 KB |
Output is correct |
14 |
Correct |
14 ms |
11356 KB |
Output is correct |
15 |
Correct |
15 ms |
11356 KB |
Output is correct |
16 |
Correct |
11 ms |
9820 KB |
Output is correct |
17 |
Correct |
103 ms |
80176 KB |
Output is correct |
18 |
Correct |
118 ms |
94548 KB |
Output is correct |
19 |
Correct |
68 ms |
63060 KB |
Output is correct |
20 |
Runtime error |
375 ms |
262144 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 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 |
13 ms |
11608 KB |
Output is correct |
12 |
Correct |
35 ms |
15512 KB |
Output is correct |
13 |
Correct |
38 ms |
15448 KB |
Output is correct |
14 |
Correct |
13 ms |
11356 KB |
Output is correct |
15 |
Correct |
18 ms |
11572 KB |
Output is correct |
16 |
Correct |
11 ms |
9820 KB |
Output is correct |
17 |
Correct |
104 ms |
80288 KB |
Output is correct |
18 |
Correct |
102 ms |
94548 KB |
Output is correct |
19 |
Correct |
71 ms |
63056 KB |
Output is correct |
20 |
Runtime error |
374 ms |
262144 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
2616 KB |
Output is correct |
11 |
Correct |
14 ms |
11612 KB |
Output is correct |
12 |
Correct |
47 ms |
15316 KB |
Output is correct |
13 |
Correct |
40 ms |
15448 KB |
Output is correct |
14 |
Correct |
13 ms |
11356 KB |
Output is correct |
15 |
Correct |
14 ms |
11356 KB |
Output is correct |
16 |
Correct |
11 ms |
9820 KB |
Output is correct |
17 |
Correct |
101 ms |
80252 KB |
Output is correct |
18 |
Correct |
101 ms |
94548 KB |
Output is correct |
19 |
Correct |
81 ms |
63224 KB |
Output is correct |
20 |
Runtime error |
389 ms |
262144 KB |
Execution killed with signal 9 |
21 |
Halted |
0 ms |
0 KB |
- |