#include <iostream>
#include <vector>
#include <set>
using namespace std;
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define MAXN 30005
int n, m, t[MAXN], s, e;
vector<int> p[MAXN];
set<int> a;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
FOR(i, 0, n - 1) t[i] = -1;
FOR(i, 0, m - 1){
int b, pi; cin >> b >> pi;
if(i == 0) s = b, t[b] = 0;
if(i == 1) e = b;
p[b].push_back(pi);
}
FOR(i, 0, n - 1) if(i != s) a.insert(i);
int c = s;
pair<int, int> nx = make_pair(-1, -1);
while(c != -1){
if(n == 0) break;
n--;
for(int i : a){
for(int j : p[c]){
if(abs(i - c) % j == 0){
t[i] = (t[i] == -1) ? (t[c] + (int)abs(i - c) / j) : min(t[i], t[c] + (int)abs(i - c) / j);
}
}
if(t[i] != -1 && (nx.first == -1 || (nx.first > t[i]))){
nx.first = t[i];
nx.second = i;
}
}
c = nx.second;
a.erase(nx.second);
nx.first = nx.second = -1;
}
cout << t[e] << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1164 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1368 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1168 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1112 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1172 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1164 KB |
Output is correct |
6 |
Correct |
1 ms |
1112 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
1112 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1288 KB |
Output is correct |
20 |
Correct |
13 ms |
1372 KB |
Output is correct |
21 |
Correct |
1 ms |
1112 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
9 ms |
1280 KB |
Output is correct |
24 |
Correct |
13 ms |
1116 KB |
Output is correct |
25 |
Correct |
13 ms |
1116 KB |
Output is correct |
26 |
Correct |
11 ms |
1116 KB |
Output is correct |
27 |
Correct |
12 ms |
1116 KB |
Output is correct |
28 |
Correct |
13 ms |
1116 KB |
Output is correct |
29 |
Correct |
14 ms |
1172 KB |
Output is correct |
30 |
Correct |
11 ms |
1116 KB |
Output is correct |
31 |
Correct |
12 ms |
1116 KB |
Output is correct |
32 |
Correct |
12 ms |
1112 KB |
Output is correct |
33 |
Correct |
20 ms |
1116 KB |
Output is correct |
34 |
Correct |
19 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1120 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1368 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
3 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1112 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
12 ms |
1352 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
9 ms |
1116 KB |
Output is correct |
24 |
Correct |
13 ms |
1116 KB |
Output is correct |
25 |
Correct |
13 ms |
1112 KB |
Output is correct |
26 |
Correct |
11 ms |
1116 KB |
Output is correct |
27 |
Correct |
12 ms |
1292 KB |
Output is correct |
28 |
Correct |
12 ms |
1116 KB |
Output is correct |
29 |
Correct |
14 ms |
1368 KB |
Output is correct |
30 |
Correct |
11 ms |
1116 KB |
Output is correct |
31 |
Correct |
12 ms |
1276 KB |
Output is correct |
32 |
Correct |
12 ms |
1116 KB |
Output is correct |
33 |
Correct |
21 ms |
1300 KB |
Output is correct |
34 |
Correct |
19 ms |
1116 KB |
Output is correct |
35 |
Correct |
42 ms |
1628 KB |
Output is correct |
36 |
Correct |
8 ms |
1372 KB |
Output is correct |
37 |
Correct |
46 ms |
1372 KB |
Output is correct |
38 |
Correct |
67 ms |
1628 KB |
Output is correct |
39 |
Correct |
66 ms |
1716 KB |
Output is correct |
40 |
Correct |
66 ms |
1624 KB |
Output is correct |
41 |
Correct |
67 ms |
1628 KB |
Output is correct |
42 |
Correct |
18 ms |
1628 KB |
Output is correct |
43 |
Correct |
25 ms |
1628 KB |
Output is correct |
44 |
Correct |
14 ms |
1628 KB |
Output is correct |
45 |
Correct |
129 ms |
1628 KB |
Output is correct |
46 |
Correct |
128 ms |
1628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
0 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
3 ms |
1236 KB |
Output is correct |
18 |
Correct |
1 ms |
1112 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
12 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
9 ms |
1116 KB |
Output is correct |
24 |
Correct |
13 ms |
1204 KB |
Output is correct |
25 |
Correct |
13 ms |
1116 KB |
Output is correct |
26 |
Correct |
11 ms |
1112 KB |
Output is correct |
27 |
Correct |
12 ms |
1116 KB |
Output is correct |
28 |
Correct |
12 ms |
1348 KB |
Output is correct |
29 |
Correct |
14 ms |
1112 KB |
Output is correct |
30 |
Correct |
10 ms |
1112 KB |
Output is correct |
31 |
Correct |
12 ms |
1116 KB |
Output is correct |
32 |
Correct |
11 ms |
1116 KB |
Output is correct |
33 |
Correct |
20 ms |
1116 KB |
Output is correct |
34 |
Correct |
19 ms |
1292 KB |
Output is correct |
35 |
Correct |
41 ms |
1372 KB |
Output is correct |
36 |
Correct |
8 ms |
1116 KB |
Output is correct |
37 |
Correct |
44 ms |
1572 KB |
Output is correct |
38 |
Correct |
68 ms |
1624 KB |
Output is correct |
39 |
Correct |
66 ms |
1716 KB |
Output is correct |
40 |
Correct |
66 ms |
1628 KB |
Output is correct |
41 |
Correct |
65 ms |
1624 KB |
Output is correct |
42 |
Correct |
18 ms |
1628 KB |
Output is correct |
43 |
Correct |
30 ms |
1500 KB |
Output is correct |
44 |
Correct |
14 ms |
1624 KB |
Output is correct |
45 |
Correct |
128 ms |
1628 KB |
Output is correct |
46 |
Correct |
129 ms |
1624 KB |
Output is correct |
47 |
Correct |
701 ms |
2396 KB |
Output is correct |
48 |
Correct |
8 ms |
2888 KB |
Output is correct |
49 |
Correct |
7 ms |
2908 KB |
Output is correct |
50 |
Correct |
6 ms |
2880 KB |
Output is correct |
51 |
Execution timed out |
1090 ms |
3544 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |