#include <bits/stdc++.h>
//#define int long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define sc second
#define endl "\n"
#define pii pair<int,int>
using namespace std;
const int MAXN = 30005;
const int mod7 = 1e9+7;
const long long inf = 1e18;
vector<vector<pii>> graf(MAXN);
vector<long long> dist(MAXN, inf);
vector<set<int>> dogos(MAXN);
set<pii> pq;
int doge0;
int doge1;
void dikstra(int nod)
{
dist[nod] = 0;
pq.insert({dist[nod], nod});
while(!pq.empty())
{
auto it = pq.begin();
int u = it-> sc;
pq.erase(it);
for(auto x: graf[u])
{
if(dist[x.fi] > dist[u] + x.sc)
{
pq.erase({dist[x.fi], x.fi});
dist[x.fi] = dist[u] + x.sc;
pq.insert({dist[x.fi], x.fi});
}
}
}
}
signed main()
{
ios_base::sync_with_stdio(false),cin.tie(0), cout.tie(0);
int tt=1;
//cin >> tt;
while(tt--)
{
int n,m;
cin >> n >> m;
for(int i=0; i<m; i++)
{
int a,b;cin >> a >> b;
if(i==0)doge0 = a;
if(i==1)doge1 = a;
dogos[a].insert(b);
}
for(int i=0; i<n; i++)
{
for(auto d: dogos[i])
{
int j = i;
while(j+ d <n)
{
j+=d;
graf[i].pb({j, (j-i)/d});
if(dogos[j].count(d))break;
}
j = i;
while(j- d >=0)
{
j-=d;
graf[i].pb({j,(i-j)/d});
if(dogos[j].count(d))break;
}
}
}
dikstra(doge0);
if(dist[doge1] == inf)cout << -1 << endl;
else cout << dist[doge1] << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2648 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2768 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
2 ms |
2648 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
2 ms |
2648 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2728 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2904 KB |
Output is correct |
19 |
Correct |
1 ms |
2648 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2904 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
5 ms |
3420 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
3160 KB |
Output is correct |
32 |
Correct |
2 ms |
3160 KB |
Output is correct |
33 |
Correct |
7 ms |
3932 KB |
Output is correct |
34 |
Correct |
6 ms |
3944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2724 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2908 KB |
Output is correct |
12 |
Correct |
1 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
2 ms |
2908 KB |
Output is correct |
15 |
Correct |
1 ms |
2908 KB |
Output is correct |
16 |
Correct |
2 ms |
2652 KB |
Output is correct |
17 |
Correct |
3 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2904 KB |
Output is correct |
20 |
Correct |
2 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2684 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2724 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
5 ms |
3416 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
3164 KB |
Output is correct |
32 |
Correct |
2 ms |
2908 KB |
Output is correct |
33 |
Correct |
6 ms |
3932 KB |
Output is correct |
34 |
Correct |
7 ms |
3932 KB |
Output is correct |
35 |
Correct |
12 ms |
4952 KB |
Output is correct |
36 |
Correct |
3 ms |
3164 KB |
Output is correct |
37 |
Correct |
14 ms |
5724 KB |
Output is correct |
38 |
Correct |
16 ms |
5724 KB |
Output is correct |
39 |
Correct |
16 ms |
5976 KB |
Output is correct |
40 |
Correct |
17 ms |
5784 KB |
Output is correct |
41 |
Correct |
16 ms |
5980 KB |
Output is correct |
42 |
Correct |
4 ms |
3032 KB |
Output is correct |
43 |
Correct |
4 ms |
2908 KB |
Output is correct |
44 |
Correct |
4 ms |
3164 KB |
Output is correct |
45 |
Correct |
20 ms |
8632 KB |
Output is correct |
46 |
Correct |
19 ms |
8552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2916 KB |
Output is correct |
2 |
Correct |
1 ms |
2664 KB |
Output is correct |
3 |
Correct |
1 ms |
2664 KB |
Output is correct |
4 |
Correct |
2 ms |
2664 KB |
Output is correct |
5 |
Correct |
1 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
1 ms |
2664 KB |
Output is correct |
8 |
Correct |
1 ms |
2652 KB |
Output is correct |
9 |
Correct |
1 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
2 ms |
2908 KB |
Output is correct |
12 |
Correct |
2 ms |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2652 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
1 ms |
2652 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
1 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2652 KB |
Output is correct |
20 |
Correct |
1 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2908 KB |
Output is correct |
29 |
Correct |
4 ms |
3420 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
3164 KB |
Output is correct |
32 |
Correct |
3 ms |
3160 KB |
Output is correct |
33 |
Correct |
6 ms |
3932 KB |
Output is correct |
34 |
Correct |
6 ms |
4032 KB |
Output is correct |
35 |
Correct |
13 ms |
4956 KB |
Output is correct |
36 |
Correct |
3 ms |
3164 KB |
Output is correct |
37 |
Correct |
13 ms |
5524 KB |
Output is correct |
38 |
Correct |
16 ms |
5980 KB |
Output is correct |
39 |
Correct |
17 ms |
5976 KB |
Output is correct |
40 |
Correct |
16 ms |
5980 KB |
Output is correct |
41 |
Correct |
16 ms |
5812 KB |
Output is correct |
42 |
Correct |
4 ms |
3164 KB |
Output is correct |
43 |
Correct |
5 ms |
2908 KB |
Output is correct |
44 |
Correct |
4 ms |
3164 KB |
Output is correct |
45 |
Correct |
19 ms |
8580 KB |
Output is correct |
46 |
Correct |
18 ms |
8616 KB |
Output is correct |
47 |
Correct |
42 ms |
12980 KB |
Output is correct |
48 |
Correct |
12 ms |
6360 KB |
Output is correct |
49 |
Correct |
9 ms |
5468 KB |
Output is correct |
50 |
Correct |
7 ms |
5048 KB |
Output is correct |
51 |
Correct |
44 ms |
9148 KB |
Output is correct |
52 |
Correct |
43 ms |
9308 KB |
Output is correct |
53 |
Correct |
17 ms |
6356 KB |
Output is correct |
54 |
Correct |
4 ms |
3676 KB |
Output is correct |
55 |
Correct |
5 ms |
4012 KB |
Output is correct |
56 |
Correct |
8 ms |
5340 KB |
Output is correct |
57 |
Correct |
46 ms |
11968 KB |
Output is correct |
58 |
Correct |
8 ms |
4308 KB |
Output is correct |
59 |
Correct |
8 ms |
4824 KB |
Output is correct |
60 |
Correct |
10 ms |
5424 KB |
Output is correct |
61 |
Correct |
10 ms |
4872 KB |
Output is correct |
62 |
Correct |
15 ms |
9432 KB |
Output is correct |
63 |
Correct |
81 ms |
25748 KB |
Output is correct |
64 |
Correct |
90 ms |
29268 KB |
Output is correct |
65 |
Correct |
111 ms |
38344 KB |
Output is correct |
66 |
Correct |
210 ms |
63040 KB |
Output is correct |
67 |
Correct |
217 ms |
63072 KB |
Output is correct |