#include<bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define REP(i, n) FOR(i, 0, n)
#define _ << " " <<
#define sz(x) ((int) x.size())
#define pb(x) push_back(x)
#define TRACE(x) cerr << #x << " = " << x << endl
typedef long long ll;
typedef pair<int, int> point;
const int mod = 1e9 + 7;
int add(int x, int y) {x += y; if(x >= mod) return x - mod; return x;}
int sub(int x, int y) {x -= y; if(x < 0) return x + mod; return x;}
int mul(int x, int y) {return (ll) x * y % mod;}
const int MAXN = 3e4 + 5;
int n, m, dist[MAXN], start[MAXN];
set<int> S[MAXN];
vector <point> E[MAXN];
void update(int pos, int range){
int cnt = 1;
for(int i = pos + range; i < n; i += range){
E[pos].pb(point(i, cnt));
cnt ++;
if(S[i].find(range) != S[i].end()) break;
}
cnt = 1;
for(int i = pos - range; i >= 0; i -= range){
E[pos].pb(point(i, cnt));
cnt ++;
if(S[i].find(range) != S[i].end()) break;
}
}
const int inf = 1e9;
int dijkstra(){
priority_queue <point> Q;
Q.push(point(0, start[0]));
vector <int> d(MAXN, inf);
d[start[0]] = 0;
while(!Q.empty()){
auto tmp = Q.top(); Q.pop();
int distance = -tmp.first;
int cvor = tmp.second;
d[cvor] = distance;
if(cvor == start[1]) break;
for(auto e : E[cvor]){
int ncvor = e.first;
int ndist = distance + e.second;
if(ndist < d[ncvor]){
d[ncvor] = ndist;
Q.push(point(-ndist, ncvor));
}
}
}
if(d[start[1]] == inf) return -1;
else return d[start[1]];
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
REP(i, m)
cin >> start[i] >> dist[i];
REP(i, m)
if(start[i] != start[1])
S[start[i]].insert(dist[i]);
REP(i, n){
if(i == start[1]) continue;
for(auto it : S[i])
update(i, it);
}
cout << dijkstra();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2552 KB |
Output is correct |
2 |
Correct |
4 ms |
2552 KB |
Output is correct |
3 |
Correct |
4 ms |
2556 KB |
Output is correct |
4 |
Correct |
4 ms |
2552 KB |
Output is correct |
5 |
Correct |
4 ms |
2552 KB |
Output is correct |
6 |
Correct |
4 ms |
2552 KB |
Output is correct |
7 |
Correct |
4 ms |
2552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2556 KB |
Output is correct |
2 |
Correct |
4 ms |
2552 KB |
Output is correct |
3 |
Correct |
4 ms |
2552 KB |
Output is correct |
4 |
Correct |
4 ms |
2552 KB |
Output is correct |
5 |
Correct |
4 ms |
2552 KB |
Output is correct |
6 |
Correct |
4 ms |
2552 KB |
Output is correct |
7 |
Correct |
4 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
2552 KB |
Output is correct |
9 |
Correct |
4 ms |
2552 KB |
Output is correct |
10 |
Correct |
4 ms |
2552 KB |
Output is correct |
11 |
Correct |
5 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2552 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2552 KB |
Output is correct |
2 |
Correct |
4 ms |
2552 KB |
Output is correct |
3 |
Correct |
4 ms |
2552 KB |
Output is correct |
4 |
Correct |
4 ms |
2552 KB |
Output is correct |
5 |
Correct |
4 ms |
2552 KB |
Output is correct |
6 |
Correct |
4 ms |
2552 KB |
Output is correct |
7 |
Correct |
4 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
2552 KB |
Output is correct |
9 |
Correct |
4 ms |
2552 KB |
Output is correct |
10 |
Correct |
7 ms |
2552 KB |
Output is correct |
11 |
Correct |
5 ms |
2680 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2552 KB |
Output is correct |
14 |
Correct |
4 ms |
2680 KB |
Output is correct |
15 |
Correct |
6 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
5 ms |
2808 KB |
Output is correct |
18 |
Correct |
5 ms |
2680 KB |
Output is correct |
19 |
Correct |
4 ms |
2680 KB |
Output is correct |
20 |
Correct |
5 ms |
2808 KB |
Output is correct |
21 |
Correct |
4 ms |
2684 KB |
Output is correct |
22 |
Correct |
4 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
5 ms |
2936 KB |
Output is correct |
25 |
Correct |
5 ms |
2804 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2684 KB |
Output is correct |
28 |
Correct |
5 ms |
2936 KB |
Output is correct |
29 |
Correct |
7 ms |
3448 KB |
Output is correct |
30 |
Correct |
5 ms |
2936 KB |
Output is correct |
31 |
Correct |
6 ms |
3064 KB |
Output is correct |
32 |
Correct |
5 ms |
2936 KB |
Output is correct |
33 |
Correct |
9 ms |
3956 KB |
Output is correct |
34 |
Correct |
8 ms |
3956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2552 KB |
Output is correct |
2 |
Correct |
4 ms |
2552 KB |
Output is correct |
3 |
Correct |
4 ms |
2552 KB |
Output is correct |
4 |
Correct |
4 ms |
2552 KB |
Output is correct |
5 |
Correct |
4 ms |
2552 KB |
Output is correct |
6 |
Correct |
4 ms |
2552 KB |
Output is correct |
7 |
Correct |
4 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
2628 KB |
Output is correct |
9 |
Correct |
4 ms |
2552 KB |
Output is correct |
10 |
Correct |
4 ms |
2680 KB |
Output is correct |
11 |
Correct |
5 ms |
2760 KB |
Output is correct |
12 |
Correct |
4 ms |
2552 KB |
Output is correct |
13 |
Correct |
4 ms |
2680 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
5 ms |
2808 KB |
Output is correct |
16 |
Correct |
5 ms |
2680 KB |
Output is correct |
17 |
Correct |
5 ms |
2936 KB |
Output is correct |
18 |
Correct |
5 ms |
2680 KB |
Output is correct |
19 |
Correct |
4 ms |
2680 KB |
Output is correct |
20 |
Correct |
5 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2680 KB |
Output is correct |
24 |
Correct |
6 ms |
2936 KB |
Output is correct |
25 |
Correct |
5 ms |
2812 KB |
Output is correct |
26 |
Correct |
5 ms |
2680 KB |
Output is correct |
27 |
Correct |
5 ms |
2680 KB |
Output is correct |
28 |
Correct |
5 ms |
2936 KB |
Output is correct |
29 |
Correct |
7 ms |
3576 KB |
Output is correct |
30 |
Correct |
5 ms |
2936 KB |
Output is correct |
31 |
Correct |
6 ms |
3064 KB |
Output is correct |
32 |
Correct |
5 ms |
2936 KB |
Output is correct |
33 |
Correct |
9 ms |
3956 KB |
Output is correct |
34 |
Correct |
7 ms |
3956 KB |
Output is correct |
35 |
Correct |
24 ms |
5240 KB |
Output is correct |
36 |
Correct |
7 ms |
3192 KB |
Output is correct |
37 |
Correct |
29 ms |
5840 KB |
Output is correct |
38 |
Correct |
32 ms |
6264 KB |
Output is correct |
39 |
Correct |
33 ms |
6136 KB |
Output is correct |
40 |
Correct |
31 ms |
6312 KB |
Output is correct |
41 |
Correct |
32 ms |
6268 KB |
Output is correct |
42 |
Correct |
10 ms |
3120 KB |
Output is correct |
43 |
Correct |
9 ms |
3064 KB |
Output is correct |
44 |
Correct |
9 ms |
3192 KB |
Output is correct |
45 |
Correct |
33 ms |
8696 KB |
Output is correct |
46 |
Correct |
32 ms |
8696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2552 KB |
Output is correct |
2 |
Correct |
4 ms |
2552 KB |
Output is correct |
3 |
Correct |
4 ms |
2552 KB |
Output is correct |
4 |
Correct |
4 ms |
2500 KB |
Output is correct |
5 |
Correct |
4 ms |
2552 KB |
Output is correct |
6 |
Correct |
4 ms |
2552 KB |
Output is correct |
7 |
Correct |
4 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
2680 KB |
Output is correct |
9 |
Correct |
4 ms |
2552 KB |
Output is correct |
10 |
Correct |
4 ms |
2552 KB |
Output is correct |
11 |
Correct |
5 ms |
2808 KB |
Output is correct |
12 |
Correct |
4 ms |
2680 KB |
Output is correct |
13 |
Correct |
4 ms |
2552 KB |
Output is correct |
14 |
Correct |
5 ms |
2808 KB |
Output is correct |
15 |
Correct |
5 ms |
2796 KB |
Output is correct |
16 |
Correct |
5 ms |
2728 KB |
Output is correct |
17 |
Correct |
5 ms |
2936 KB |
Output is correct |
18 |
Correct |
4 ms |
2680 KB |
Output is correct |
19 |
Correct |
4 ms |
2680 KB |
Output is correct |
20 |
Correct |
5 ms |
2680 KB |
Output is correct |
21 |
Correct |
4 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2680 KB |
Output is correct |
23 |
Correct |
5 ms |
2800 KB |
Output is correct |
24 |
Correct |
6 ms |
2936 KB |
Output is correct |
25 |
Correct |
5 ms |
2808 KB |
Output is correct |
26 |
Correct |
5 ms |
2808 KB |
Output is correct |
27 |
Correct |
5 ms |
2680 KB |
Output is correct |
28 |
Correct |
5 ms |
2936 KB |
Output is correct |
29 |
Correct |
7 ms |
3448 KB |
Output is correct |
30 |
Correct |
5 ms |
2936 KB |
Output is correct |
31 |
Correct |
6 ms |
3064 KB |
Output is correct |
32 |
Correct |
6 ms |
2936 KB |
Output is correct |
33 |
Correct |
9 ms |
3956 KB |
Output is correct |
34 |
Correct |
8 ms |
3956 KB |
Output is correct |
35 |
Correct |
24 ms |
5240 KB |
Output is correct |
36 |
Correct |
6 ms |
3064 KB |
Output is correct |
37 |
Correct |
27 ms |
5884 KB |
Output is correct |
38 |
Correct |
32 ms |
6236 KB |
Output is correct |
39 |
Correct |
31 ms |
6264 KB |
Output is correct |
40 |
Correct |
33 ms |
6264 KB |
Output is correct |
41 |
Correct |
31 ms |
6264 KB |
Output is correct |
42 |
Correct |
10 ms |
3192 KB |
Output is correct |
43 |
Correct |
9 ms |
3064 KB |
Output is correct |
44 |
Correct |
9 ms |
3192 KB |
Output is correct |
45 |
Correct |
35 ms |
8824 KB |
Output is correct |
46 |
Correct |
32 ms |
8824 KB |
Output is correct |
47 |
Correct |
55 ms |
13120 KB |
Output is correct |
48 |
Correct |
23 ms |
6516 KB |
Output is correct |
49 |
Correct |
19 ms |
5496 KB |
Output is correct |
50 |
Correct |
16 ms |
5112 KB |
Output is correct |
51 |
Correct |
41 ms |
8576 KB |
Output is correct |
52 |
Correct |
44 ms |
8952 KB |
Output is correct |
53 |
Correct |
19 ms |
5420 KB |
Output is correct |
54 |
Correct |
6 ms |
2940 KB |
Output is correct |
55 |
Correct |
7 ms |
3316 KB |
Output is correct |
56 |
Correct |
18 ms |
5368 KB |
Output is correct |
57 |
Correct |
23 ms |
11512 KB |
Output is correct |
58 |
Correct |
12 ms |
3824 KB |
Output is correct |
59 |
Correct |
16 ms |
4340 KB |
Output is correct |
60 |
Correct |
15 ms |
4852 KB |
Output is correct |
61 |
Correct |
16 ms |
4088 KB |
Output is correct |
62 |
Correct |
38 ms |
9336 KB |
Output is correct |
63 |
Correct |
97 ms |
25324 KB |
Output is correct |
64 |
Correct |
96 ms |
28908 KB |
Output is correct |
65 |
Correct |
128 ms |
38128 KB |
Output is correct |
66 |
Correct |
264 ms |
64756 KB |
Output is correct |
67 |
Correct |
235 ms |
64736 KB |
Output is correct |