//Jakarta skysrcapers
#include <bits/stdc++.h>
#define INF MOD
#define MOD 100000007
#define MAX_N 30005
#define MAX_M 30005
using namespace std;
int N, M;
int source, dest;
struct edge{
int d, w;
};
vector<int> soli[MAX_N];
vector<edge> adj[MAX_N];
int dist[MAX_N];
bool mark[MAX_N];
void dijkstra(int s){
memset(dist, INF, sizeof(dist));
memset(mark, 0, sizeof(mark));
dist[s]=0;
priority_queue<pair<int, int> > pq;
pq.emplace(0, s);
while(!pq.empty()){
auto cur=pq.top();
pq.pop();
int v=cur.second;
if(mark[v])continue;
mark[v]=1;
int d=soli[v].size();
for(int j=0; j<d; ++j){
int p=soli[v][j];
int sk=v-p;
while(sk>=0){
int w=(v-sk)/p;
if(!mark[sk] && dist[sk]>dist[v]+w){
dist[sk]=dist[v]+w;
pq.emplace(-1*(dist[v]+w), sk);
}
sk-=p;
}
sk=v+p;
while(sk<N){
int w=(sk-v)/p;
if(!mark[sk] && dist[sk]>dist[v]+w){
dist[sk]=dist[v]+w;
pq.emplace(-1*(dist[v]+w), sk);
}
sk+=p;
}
}
}
}
bool cmp(int i, int j){return i>j;}
int main(){
ios::sync_with_stdio(false);
cin>>N>>M;
//assert(N<=29000);
int b;
cin>>source>>b;
soli[source].push_back(b);
cin>>dest>>b;
soli[dest].push_back(b);
for(int i=2; i<M; ++i){
int p;
cin>>b>>p;
soli[b].push_back(p);
}
for(int i=0; i<N; ++i){
sort(soli[i].begin(), soli[i].end(), cmp);
}
dijkstra(source);
if(dist[dest]>100000000)cout<<"-1"<<endl;
else
cout<<dist[dest];
return 0;
}
/*
5 3
0 2
1 1
4 1
*/
/*
4 4
0 1 1
1 2 2
2 3 1
1 3 4
0 3
5 6
0 1 1
1 2 2
2 3 1
1 3 4
0 4 2
4 3 1
0 3
3
3 2
1 2 5
2 3 7
1 3
3 3
1 2 4
1 3 7
2 3 1
1 3
3 1
1 2 4
1 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1920 KB |
Output is correct |
2 |
Correct |
18 ms |
1920 KB |
Output is correct |
3 |
Correct |
4 ms |
1920 KB |
Output is correct |
4 |
Correct |
3 ms |
1920 KB |
Output is correct |
5 |
Correct |
3 ms |
1920 KB |
Output is correct |
6 |
Correct |
4 ms |
1920 KB |
Output is correct |
7 |
Correct |
4 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1920 KB |
Output is correct |
2 |
Correct |
3 ms |
1920 KB |
Output is correct |
3 |
Correct |
4 ms |
1920 KB |
Output is correct |
4 |
Correct |
3 ms |
1920 KB |
Output is correct |
5 |
Correct |
3 ms |
1920 KB |
Output is correct |
6 |
Correct |
3 ms |
1920 KB |
Output is correct |
7 |
Correct |
3 ms |
1920 KB |
Output is correct |
8 |
Correct |
4 ms |
1920 KB |
Output is correct |
9 |
Correct |
5 ms |
1920 KB |
Output is correct |
10 |
Correct |
4 ms |
1920 KB |
Output is correct |
11 |
Correct |
5 ms |
1920 KB |
Output is correct |
12 |
Correct |
6 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
1920 KB |
Output is correct |
14 |
Correct |
5 ms |
1920 KB |
Output is correct |
15 |
Correct |
4 ms |
1920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1920 KB |
Output is correct |
2 |
Correct |
3 ms |
1920 KB |
Output is correct |
3 |
Correct |
4 ms |
1920 KB |
Output is correct |
4 |
Correct |
4 ms |
1920 KB |
Output is correct |
5 |
Correct |
4 ms |
1920 KB |
Output is correct |
6 |
Correct |
4 ms |
1920 KB |
Output is correct |
7 |
Correct |
4 ms |
1920 KB |
Output is correct |
8 |
Correct |
4 ms |
1920 KB |
Output is correct |
9 |
Correct |
4 ms |
1920 KB |
Output is correct |
10 |
Correct |
4 ms |
1792 KB |
Output is correct |
11 |
Correct |
5 ms |
1920 KB |
Output is correct |
12 |
Correct |
4 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
1920 KB |
Output is correct |
14 |
Correct |
4 ms |
1920 KB |
Output is correct |
15 |
Correct |
4 ms |
1920 KB |
Output is correct |
16 |
Correct |
4 ms |
1920 KB |
Output is correct |
17 |
Correct |
4 ms |
1920 KB |
Output is correct |
18 |
Correct |
5 ms |
1920 KB |
Output is correct |
19 |
Correct |
4 ms |
1920 KB |
Output is correct |
20 |
Correct |
18 ms |
1920 KB |
Output is correct |
21 |
Correct |
5 ms |
1920 KB |
Output is correct |
22 |
Correct |
4 ms |
1920 KB |
Output is correct |
23 |
Correct |
5 ms |
1920 KB |
Output is correct |
24 |
Correct |
5 ms |
2008 KB |
Output is correct |
25 |
Correct |
5 ms |
1920 KB |
Output is correct |
26 |
Correct |
16 ms |
1920 KB |
Output is correct |
27 |
Correct |
16 ms |
1920 KB |
Output is correct |
28 |
Correct |
5 ms |
1920 KB |
Output is correct |
29 |
Correct |
5 ms |
2048 KB |
Output is correct |
30 |
Correct |
4 ms |
1920 KB |
Output is correct |
31 |
Correct |
4 ms |
1920 KB |
Output is correct |
32 |
Correct |
5 ms |
1920 KB |
Output is correct |
33 |
Correct |
5 ms |
2048 KB |
Output is correct |
34 |
Correct |
6 ms |
2048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1920 KB |
Output is correct |
2 |
Correct |
4 ms |
1920 KB |
Output is correct |
3 |
Correct |
3 ms |
1920 KB |
Output is correct |
4 |
Correct |
4 ms |
1920 KB |
Output is correct |
5 |
Correct |
4 ms |
1920 KB |
Output is correct |
6 |
Correct |
5 ms |
1920 KB |
Output is correct |
7 |
Correct |
4 ms |
1920 KB |
Output is correct |
8 |
Correct |
5 ms |
1920 KB |
Output is correct |
9 |
Correct |
4 ms |
1920 KB |
Output is correct |
10 |
Correct |
4 ms |
1920 KB |
Output is correct |
11 |
Correct |
6 ms |
1920 KB |
Output is correct |
12 |
Correct |
4 ms |
1920 KB |
Output is correct |
13 |
Correct |
5 ms |
1920 KB |
Output is correct |
14 |
Correct |
4 ms |
1920 KB |
Output is correct |
15 |
Correct |
5 ms |
1920 KB |
Output is correct |
16 |
Correct |
4 ms |
1920 KB |
Output is correct |
17 |
Correct |
4 ms |
1920 KB |
Output is correct |
18 |
Correct |
4 ms |
1920 KB |
Output is correct |
19 |
Correct |
4 ms |
1920 KB |
Output is correct |
20 |
Correct |
20 ms |
1920 KB |
Output is correct |
21 |
Correct |
4 ms |
1920 KB |
Output is correct |
22 |
Correct |
4 ms |
1920 KB |
Output is correct |
23 |
Correct |
5 ms |
1792 KB |
Output is correct |
24 |
Correct |
6 ms |
1920 KB |
Output is correct |
25 |
Correct |
4 ms |
2048 KB |
Output is correct |
26 |
Correct |
14 ms |
1920 KB |
Output is correct |
27 |
Correct |
16 ms |
1920 KB |
Output is correct |
28 |
Correct |
6 ms |
1920 KB |
Output is correct |
29 |
Correct |
5 ms |
2048 KB |
Output is correct |
30 |
Correct |
5 ms |
1920 KB |
Output is correct |
31 |
Correct |
4 ms |
1920 KB |
Output is correct |
32 |
Correct |
4 ms |
2048 KB |
Output is correct |
33 |
Correct |
5 ms |
2048 KB |
Output is correct |
34 |
Correct |
6 ms |
2048 KB |
Output is correct |
35 |
Correct |
15 ms |
2176 KB |
Output is correct |
36 |
Correct |
5 ms |
1920 KB |
Output is correct |
37 |
Correct |
10 ms |
2176 KB |
Output is correct |
38 |
Correct |
12 ms |
2176 KB |
Output is correct |
39 |
Correct |
12 ms |
2176 KB |
Output is correct |
40 |
Correct |
16 ms |
2176 KB |
Output is correct |
41 |
Correct |
13 ms |
2176 KB |
Output is correct |
42 |
Correct |
179 ms |
2176 KB |
Output is correct |
43 |
Correct |
199 ms |
2176 KB |
Output is correct |
44 |
Correct |
147 ms |
2296 KB |
Output is correct |
45 |
Correct |
19 ms |
2124 KB |
Output is correct |
46 |
Correct |
13 ms |
2068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1920 KB |
Output is correct |
2 |
Correct |
4 ms |
1920 KB |
Output is correct |
3 |
Correct |
4 ms |
1920 KB |
Output is correct |
4 |
Correct |
3 ms |
1920 KB |
Output is correct |
5 |
Correct |
4 ms |
1920 KB |
Output is correct |
6 |
Correct |
3 ms |
1920 KB |
Output is correct |
7 |
Correct |
4 ms |
1920 KB |
Output is correct |
8 |
Correct |
4 ms |
1920 KB |
Output is correct |
9 |
Correct |
4 ms |
1920 KB |
Output is correct |
10 |
Correct |
4 ms |
1920 KB |
Output is correct |
11 |
Correct |
6 ms |
1920 KB |
Output is correct |
12 |
Correct |
4 ms |
1920 KB |
Output is correct |
13 |
Correct |
4 ms |
1920 KB |
Output is correct |
14 |
Correct |
5 ms |
1888 KB |
Output is correct |
15 |
Correct |
4 ms |
1920 KB |
Output is correct |
16 |
Correct |
4 ms |
1920 KB |
Output is correct |
17 |
Correct |
4 ms |
1920 KB |
Output is correct |
18 |
Correct |
4 ms |
1920 KB |
Output is correct |
19 |
Correct |
4 ms |
1920 KB |
Output is correct |
20 |
Correct |
18 ms |
1920 KB |
Output is correct |
21 |
Correct |
4 ms |
1920 KB |
Output is correct |
22 |
Correct |
5 ms |
1920 KB |
Output is correct |
23 |
Correct |
4 ms |
1896 KB |
Output is correct |
24 |
Correct |
6 ms |
1920 KB |
Output is correct |
25 |
Correct |
4 ms |
1920 KB |
Output is correct |
26 |
Correct |
14 ms |
1920 KB |
Output is correct |
27 |
Correct |
18 ms |
1920 KB |
Output is correct |
28 |
Correct |
5 ms |
2048 KB |
Output is correct |
29 |
Correct |
5 ms |
2048 KB |
Output is correct |
30 |
Correct |
5 ms |
1920 KB |
Output is correct |
31 |
Correct |
4 ms |
1920 KB |
Output is correct |
32 |
Correct |
5 ms |
1920 KB |
Output is correct |
33 |
Correct |
5 ms |
2048 KB |
Output is correct |
34 |
Correct |
5 ms |
2048 KB |
Output is correct |
35 |
Correct |
11 ms |
2176 KB |
Output is correct |
36 |
Correct |
5 ms |
1920 KB |
Output is correct |
37 |
Correct |
13 ms |
2168 KB |
Output is correct |
38 |
Correct |
14 ms |
2176 KB |
Output is correct |
39 |
Correct |
16 ms |
2304 KB |
Output is correct |
40 |
Correct |
14 ms |
2176 KB |
Output is correct |
41 |
Correct |
17 ms |
2276 KB |
Output is correct |
42 |
Correct |
195 ms |
2188 KB |
Output is correct |
43 |
Correct |
204 ms |
2188 KB |
Output is correct |
44 |
Correct |
147 ms |
2176 KB |
Output is correct |
45 |
Correct |
13 ms |
2176 KB |
Output is correct |
46 |
Correct |
12 ms |
2176 KB |
Output is correct |
47 |
Correct |
29 ms |
2832 KB |
Output is correct |
48 |
Correct |
12 ms |
2304 KB |
Output is correct |
49 |
Correct |
11 ms |
2304 KB |
Output is correct |
50 |
Correct |
7 ms |
2304 KB |
Output is correct |
51 |
Correct |
42 ms |
3704 KB |
Output is correct |
52 |
Correct |
38 ms |
3196 KB |
Output is correct |
53 |
Correct |
29 ms |
3196 KB |
Output is correct |
54 |
Correct |
4 ms |
2148 KB |
Output is correct |
55 |
Correct |
5 ms |
2176 KB |
Output is correct |
56 |
Execution timed out |
1062 ms |
3200 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |