#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1e9+7;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t z;
const int mxn = 4e4;
int n, m;
ll b[mxn], p[mxn];
ll dist[mxn];
vector<ll> g[mxn];
bool vis[mxn];
bool con[300][mxn];
void solve(){
cin >> n >> m;
fr(i, 0, m){
cin >> b[i] >> p[i];
if(p[i] < 300){
if(!con[p[i]][b[i]]){
con[p[i]][b[i]] = true;
g[b[i]].pb(p[i]);
}
}
else{
g[b[i]].pb(p[i]);
}
}
fr(i, 0, n) dist[i] = inf;
pq <pair<ll,int> > Q;
Q.push({0, b[0]});
dist[b[0]] = 0;
while(!Q.empty()){
int c = Q.top().nd;
ll w = Q.top().st;
Q.pop();
if(c == b[1]){
cout<<-w<<endl;
return;
}
if(vis[c]) continue;
vis[c] = true;
for(auto u : g[c]){
for(int i = c + u; i < n; i += u){
if(dist[c] + (i-c)/u < dist[i]){
dist[i] = dist[c] + (i-c)/u;
Q.push({-dist[i], i});
}
if(u < 300 && con[u][i]) break;
}
for(int i = c - u; i >=0; i -= u){
if(dist[c] + (c-i)/u < dist[i]){
dist[i] = dist[c] + (c-i)/u;
Q.push({-dist[i], i});
}
if(u < 300 && con[u][i]) break;
}
}
}
cout<<-1<<endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1232 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
2 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
2 ms |
1612 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1612 KB |
Output is correct |
15 |
Correct |
2 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1284 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
2 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
2 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1300 KB |
Output is correct |
10 |
Correct |
1 ms |
1612 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1612 KB |
Output is correct |
15 |
Correct |
2 ms |
1612 KB |
Output is correct |
16 |
Correct |
2 ms |
2052 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
2 ms |
1868 KB |
Output is correct |
19 |
Correct |
1 ms |
1612 KB |
Output is correct |
20 |
Correct |
2 ms |
1356 KB |
Output is correct |
21 |
Correct |
2 ms |
1740 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
2 ms |
1868 KB |
Output is correct |
24 |
Correct |
3 ms |
2252 KB |
Output is correct |
25 |
Correct |
2 ms |
1356 KB |
Output is correct |
26 |
Correct |
2 ms |
1740 KB |
Output is correct |
27 |
Correct |
2 ms |
1612 KB |
Output is correct |
28 |
Correct |
2 ms |
1996 KB |
Output is correct |
29 |
Correct |
3 ms |
1640 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
2 ms |
1484 KB |
Output is correct |
32 |
Correct |
2 ms |
1484 KB |
Output is correct |
33 |
Correct |
3 ms |
1868 KB |
Output is correct |
34 |
Correct |
4 ms |
1996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
2 ms |
1228 KB |
Output is correct |
5 |
Correct |
2 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
2 ms |
1612 KB |
Output is correct |
11 |
Correct |
2 ms |
1744 KB |
Output is correct |
12 |
Correct |
2 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1612 KB |
Output is correct |
15 |
Correct |
2 ms |
1612 KB |
Output is correct |
16 |
Correct |
2 ms |
2124 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
2 ms |
1868 KB |
Output is correct |
19 |
Correct |
2 ms |
1612 KB |
Output is correct |
20 |
Correct |
2 ms |
1356 KB |
Output is correct |
21 |
Correct |
3 ms |
1740 KB |
Output is correct |
22 |
Correct |
2 ms |
1612 KB |
Output is correct |
23 |
Correct |
2 ms |
1868 KB |
Output is correct |
24 |
Correct |
3 ms |
2252 KB |
Output is correct |
25 |
Correct |
2 ms |
1360 KB |
Output is correct |
26 |
Correct |
2 ms |
1748 KB |
Output is correct |
27 |
Correct |
2 ms |
1612 KB |
Output is correct |
28 |
Correct |
2 ms |
1996 KB |
Output is correct |
29 |
Correct |
3 ms |
1612 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
2 ms |
1484 KB |
Output is correct |
32 |
Correct |
2 ms |
1484 KB |
Output is correct |
33 |
Correct |
4 ms |
1872 KB |
Output is correct |
34 |
Correct |
3 ms |
1996 KB |
Output is correct |
35 |
Correct |
9 ms |
3660 KB |
Output is correct |
36 |
Correct |
3 ms |
2636 KB |
Output is correct |
37 |
Correct |
7 ms |
3788 KB |
Output is correct |
38 |
Correct |
11 ms |
4044 KB |
Output is correct |
39 |
Correct |
9 ms |
3916 KB |
Output is correct |
40 |
Correct |
10 ms |
4044 KB |
Output is correct |
41 |
Correct |
10 ms |
4044 KB |
Output is correct |
42 |
Correct |
6 ms |
2252 KB |
Output is correct |
43 |
Correct |
6 ms |
1996 KB |
Output is correct |
44 |
Correct |
7 ms |
1740 KB |
Output is correct |
45 |
Correct |
13 ms |
3380 KB |
Output is correct |
46 |
Correct |
12 ms |
3448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1228 KB |
Output is correct |
2 |
Correct |
1 ms |
1228 KB |
Output is correct |
3 |
Correct |
1 ms |
1228 KB |
Output is correct |
4 |
Correct |
1 ms |
1228 KB |
Output is correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Correct |
1 ms |
1228 KB |
Output is correct |
8 |
Correct |
1 ms |
1356 KB |
Output is correct |
9 |
Correct |
1 ms |
1356 KB |
Output is correct |
10 |
Correct |
1 ms |
1612 KB |
Output is correct |
11 |
Correct |
2 ms |
1740 KB |
Output is correct |
12 |
Correct |
1 ms |
1356 KB |
Output is correct |
13 |
Correct |
1 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
1612 KB |
Output is correct |
15 |
Correct |
3 ms |
1612 KB |
Output is correct |
16 |
Correct |
2 ms |
2124 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
2 ms |
1868 KB |
Output is correct |
19 |
Correct |
1 ms |
1612 KB |
Output is correct |
20 |
Correct |
2 ms |
1356 KB |
Output is correct |
21 |
Correct |
2 ms |
1740 KB |
Output is correct |
22 |
Correct |
1 ms |
1612 KB |
Output is correct |
23 |
Correct |
3 ms |
1868 KB |
Output is correct |
24 |
Correct |
3 ms |
2252 KB |
Output is correct |
25 |
Correct |
2 ms |
1356 KB |
Output is correct |
26 |
Correct |
2 ms |
1740 KB |
Output is correct |
27 |
Correct |
2 ms |
1612 KB |
Output is correct |
28 |
Correct |
2 ms |
1996 KB |
Output is correct |
29 |
Correct |
3 ms |
1612 KB |
Output is correct |
30 |
Correct |
2 ms |
1356 KB |
Output is correct |
31 |
Correct |
2 ms |
1484 KB |
Output is correct |
32 |
Correct |
2 ms |
1484 KB |
Output is correct |
33 |
Correct |
4 ms |
1868 KB |
Output is correct |
34 |
Correct |
3 ms |
1996 KB |
Output is correct |
35 |
Correct |
9 ms |
3660 KB |
Output is correct |
36 |
Correct |
3 ms |
2636 KB |
Output is correct |
37 |
Correct |
8 ms |
3788 KB |
Output is correct |
38 |
Correct |
12 ms |
4044 KB |
Output is correct |
39 |
Correct |
9 ms |
3916 KB |
Output is correct |
40 |
Correct |
9 ms |
4048 KB |
Output is correct |
41 |
Correct |
10 ms |
4044 KB |
Output is correct |
42 |
Correct |
6 ms |
2252 KB |
Output is correct |
43 |
Correct |
6 ms |
2060 KB |
Output is correct |
44 |
Correct |
7 ms |
1740 KB |
Output is correct |
45 |
Correct |
11 ms |
3372 KB |
Output is correct |
46 |
Correct |
12 ms |
3404 KB |
Output is correct |
47 |
Correct |
13 ms |
6864 KB |
Output is correct |
48 |
Correct |
8 ms |
3292 KB |
Output is correct |
49 |
Correct |
7 ms |
2892 KB |
Output is correct |
50 |
Correct |
6 ms |
2764 KB |
Output is correct |
51 |
Correct |
18 ms |
4820 KB |
Output is correct |
52 |
Correct |
21 ms |
4936 KB |
Output is correct |
53 |
Correct |
10 ms |
3020 KB |
Output is correct |
54 |
Correct |
3 ms |
1872 KB |
Output is correct |
55 |
Correct |
3 ms |
1872 KB |
Output is correct |
56 |
Correct |
10 ms |
2892 KB |
Output is correct |
57 |
Correct |
3 ms |
2760 KB |
Output is correct |
58 |
Correct |
8 ms |
2384 KB |
Output is correct |
59 |
Correct |
9 ms |
2812 KB |
Output is correct |
60 |
Correct |
9 ms |
3540 KB |
Output is correct |
61 |
Correct |
11 ms |
2956 KB |
Output is correct |
62 |
Correct |
15 ms |
4044 KB |
Output is correct |
63 |
Correct |
39 ms |
5312 KB |
Output is correct |
64 |
Correct |
39 ms |
5192 KB |
Output is correct |
65 |
Correct |
52 ms |
5824 KB |
Output is correct |
66 |
Correct |
80 ms |
7828 KB |
Output is correct |
67 |
Correct |
71 ms |
7788 KB |
Output is correct |