#include <bits/stdc++.h>
#define fi first
#define si second
#define int long long
using namespace std;
const int N = 5e4 + 4;
int dist[N],n,m;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
vector<int> g[N];
pair<int,int> a[N];
signed main()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
cin >> a[i].fi >> a[i].si;
a[i].fi++;
g[a[i].fi].push_back(a[i].si);
}
for(int i = 1; i <= n; i++) dist[i] = 1e18;
q.push({0,a[1].fi});
dist[a[1].first] = 0;
while(!q.empty())
{
pair<int,int> node = q.top();q.pop();
int u = node.si, d = node.fi;
if(d >= 3000) {continue;}
if(dist[u] < d) continue;
for(int p : g[u])
{ int t = d;
for(int i = u + p; i <= n; i += p)
{ t++;
if(dist[i] > t)
{
dist[i] = t;
q.push({dist[i],i});
}
}
t = d;
for(int i = u - p; i > 0; i -= p)
{
t++;
if(dist[i] > t)
{
dist[i] = t;
q.push({dist[i],i});
}
}
}
}
if(dist[a[2].first] == 1e18) cout << -1;
else cout << dist[a[2].first];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2596 KB |
Output is correct |
12 |
Correct |
2 ms |
2384 KB |
Output is correct |
13 |
Correct |
1 ms |
2384 KB |
Output is correct |
14 |
Correct |
2 ms |
2384 KB |
Output is correct |
15 |
Correct |
1 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2552 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
1 ms |
2384 KB |
Output is correct |
12 |
Correct |
2 ms |
2552 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
2 ms |
2552 KB |
Output is correct |
15 |
Correct |
2 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
2 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
4 ms |
2652 KB |
Output is correct |
21 |
Correct |
2 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
1 ms |
2384 KB |
Output is correct |
24 |
Correct |
2 ms |
2384 KB |
Output is correct |
25 |
Correct |
2 ms |
2640 KB |
Output is correct |
26 |
Correct |
4 ms |
2384 KB |
Output is correct |
27 |
Incorrect |
2 ms |
2384 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
1 ms |
2384 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2384 KB |
Output is correct |
11 |
Correct |
2 ms |
2384 KB |
Output is correct |
12 |
Correct |
2 ms |
2556 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2384 KB |
Output is correct |
15 |
Correct |
2 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
2 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2384 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
4 ms |
2824 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
2 ms |
2384 KB |
Output is correct |
24 |
Correct |
2 ms |
2384 KB |
Output is correct |
25 |
Correct |
2 ms |
2640 KB |
Output is correct |
26 |
Correct |
3 ms |
2384 KB |
Output is correct |
27 |
Incorrect |
1 ms |
2384 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2384 KB |
Output is correct |
2 |
Correct |
1 ms |
2384 KB |
Output is correct |
3 |
Correct |
1 ms |
2384 KB |
Output is correct |
4 |
Correct |
1 ms |
2384 KB |
Output is correct |
5 |
Correct |
1 ms |
2384 KB |
Output is correct |
6 |
Correct |
1 ms |
2384 KB |
Output is correct |
7 |
Correct |
2 ms |
2556 KB |
Output is correct |
8 |
Correct |
1 ms |
2384 KB |
Output is correct |
9 |
Correct |
1 ms |
2384 KB |
Output is correct |
10 |
Correct |
1 ms |
2552 KB |
Output is correct |
11 |
Correct |
2 ms |
2384 KB |
Output is correct |
12 |
Correct |
2 ms |
2384 KB |
Output is correct |
13 |
Correct |
2 ms |
2384 KB |
Output is correct |
14 |
Correct |
1 ms |
2552 KB |
Output is correct |
15 |
Correct |
2 ms |
2384 KB |
Output is correct |
16 |
Correct |
1 ms |
2384 KB |
Output is correct |
17 |
Correct |
2 ms |
2384 KB |
Output is correct |
18 |
Correct |
1 ms |
2552 KB |
Output is correct |
19 |
Correct |
1 ms |
2384 KB |
Output is correct |
20 |
Correct |
4 ms |
2640 KB |
Output is correct |
21 |
Correct |
1 ms |
2384 KB |
Output is correct |
22 |
Correct |
1 ms |
2384 KB |
Output is correct |
23 |
Correct |
2 ms |
2384 KB |
Output is correct |
24 |
Correct |
3 ms |
2384 KB |
Output is correct |
25 |
Correct |
2 ms |
2812 KB |
Output is correct |
26 |
Correct |
3 ms |
2384 KB |
Output is correct |
27 |
Incorrect |
2 ms |
2384 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |