#include <cstdio>
#include <queue>
#include <algorithm>
#include <cstring>
using namespace std;
typedef pair<int,int> pp;
typedef vector<int> vec;
typedef vector<vec> mat;
#define pb push_back
int N,M;
int a[30010],P[30010];
int dist[30010];
bool chk[30010];
priority_queue<pp,vector<pp>,greater<pp> > Q;
mat path,doge;
int main(){
//freopen(".in","r",stdin);
scanf("%d%d",&N,&M);
path.assign(M+1,vec());
doge.assign(N,vec());
for(int i=0;i<M;i++){
scanf("%d%d",&a[i],&P[i]);
doge[a[i]].push_back(P[i]);
}
memset(dist,0x3f,sizeof(dist));
int INF=dist[0];
dist[a[0]]=0;
Q.push(pp(0,a[0]));
int v;
while(!Q.empty()){
do{
v=Q.top().second;Q.pop();
}while(!Q.empty()&&chk[v]);
chk[v]=1;
if(v==a[1])break;
for(int k=0;k<doge[v].size();k++){
int p=doge[v][k];
for(int i=0;i<N;i++){
if(abs(v-i)%p==0){
if(dist[i]>dist[v]+abs(v-i)/p){
dist[i]=dist[v]+abs(v-i)/p;
Q.push(pp(dist[i],i));
}
}
}
}
}
if(dist[a[1]]==INF){
puts("-1");
}else{
printf("%d",dist[a[1]]);
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
8 |
Correct |
0 ms |
1960 KB |
Output is correct |
9 |
Correct |
0 ms |
1960 KB |
Output is correct |
10 |
Correct |
0 ms |
1960 KB |
Output is correct |
11 |
Correct |
0 ms |
1960 KB |
Output is correct |
12 |
Correct |
0 ms |
1960 KB |
Output is correct |
13 |
Correct |
2 ms |
1960 KB |
Output is correct |
14 |
Correct |
2 ms |
1960 KB |
Output is correct |
15 |
Correct |
2 ms |
1960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
8 |
Correct |
0 ms |
1960 KB |
Output is correct |
9 |
Correct |
0 ms |
1960 KB |
Output is correct |
10 |
Correct |
0 ms |
1960 KB |
Output is correct |
11 |
Correct |
0 ms |
1960 KB |
Output is correct |
12 |
Correct |
0 ms |
1960 KB |
Output is correct |
13 |
Correct |
0 ms |
1960 KB |
Output is correct |
14 |
Correct |
0 ms |
1960 KB |
Output is correct |
15 |
Correct |
0 ms |
1960 KB |
Output is correct |
16 |
Correct |
0 ms |
1960 KB |
Output is correct |
17 |
Correct |
6 ms |
1960 KB |
Output is correct |
18 |
Correct |
0 ms |
1960 KB |
Output is correct |
19 |
Correct |
0 ms |
1960 KB |
Output is correct |
20 |
Correct |
23 ms |
2092 KB |
Output is correct |
21 |
Correct |
0 ms |
1960 KB |
Output is correct |
22 |
Correct |
0 ms |
1960 KB |
Output is correct |
23 |
Correct |
9 ms |
1960 KB |
Output is correct |
24 |
Correct |
20 ms |
2092 KB |
Output is correct |
25 |
Correct |
5 ms |
2092 KB |
Output is correct |
26 |
Correct |
0 ms |
1960 KB |
Output is correct |
27 |
Correct |
3 ms |
1960 KB |
Output is correct |
28 |
Correct |
24 ms |
2092 KB |
Output is correct |
29 |
Correct |
12 ms |
2224 KB |
Output is correct |
30 |
Correct |
4 ms |
2144 KB |
Output is correct |
31 |
Correct |
9 ms |
2092 KB |
Output is correct |
32 |
Correct |
8 ms |
2152 KB |
Output is correct |
33 |
Correct |
29 ms |
2228 KB |
Output is correct |
34 |
Correct |
28 ms |
2232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
8 |
Correct |
0 ms |
1960 KB |
Output is correct |
9 |
Correct |
0 ms |
1960 KB |
Output is correct |
10 |
Correct |
0 ms |
1960 KB |
Output is correct |
11 |
Correct |
0 ms |
1960 KB |
Output is correct |
12 |
Correct |
0 ms |
1960 KB |
Output is correct |
13 |
Correct |
0 ms |
1960 KB |
Output is correct |
14 |
Correct |
0 ms |
1960 KB |
Output is correct |
15 |
Correct |
2 ms |
1960 KB |
Output is correct |
16 |
Correct |
0 ms |
1960 KB |
Output is correct |
17 |
Correct |
6 ms |
1960 KB |
Output is correct |
18 |
Correct |
0 ms |
1960 KB |
Output is correct |
19 |
Correct |
0 ms |
1960 KB |
Output is correct |
20 |
Correct |
23 ms |
2092 KB |
Output is correct |
21 |
Correct |
0 ms |
1960 KB |
Output is correct |
22 |
Correct |
0 ms |
1960 KB |
Output is correct |
23 |
Correct |
9 ms |
1960 KB |
Output is correct |
24 |
Correct |
13 ms |
2092 KB |
Output is correct |
25 |
Correct |
5 ms |
2092 KB |
Output is correct |
26 |
Correct |
0 ms |
1960 KB |
Output is correct |
27 |
Correct |
0 ms |
1960 KB |
Output is correct |
28 |
Correct |
24 ms |
2092 KB |
Output is correct |
29 |
Correct |
12 ms |
2224 KB |
Output is correct |
30 |
Correct |
0 ms |
2144 KB |
Output is correct |
31 |
Correct |
9 ms |
2092 KB |
Output is correct |
32 |
Correct |
7 ms |
2152 KB |
Output is correct |
33 |
Correct |
29 ms |
2228 KB |
Output is correct |
34 |
Correct |
29 ms |
2232 KB |
Output is correct |
35 |
Correct |
190 ms |
2828 KB |
Output is correct |
36 |
Correct |
9 ms |
2096 KB |
Output is correct |
37 |
Correct |
137 ms |
2692 KB |
Output is correct |
38 |
Correct |
103 ms |
3072 KB |
Output is correct |
39 |
Correct |
11 ms |
2928 KB |
Output is correct |
40 |
Correct |
10 ms |
2928 KB |
Output is correct |
41 |
Correct |
51 ms |
3072 KB |
Output is correct |
42 |
Correct |
7 ms |
2844 KB |
Output is correct |
43 |
Correct |
9 ms |
2844 KB |
Output is correct |
44 |
Correct |
337 ms |
2840 KB |
Output is correct |
45 |
Correct |
390 ms |
3084 KB |
Output is correct |
46 |
Correct |
382 ms |
3080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1960 KB |
Output is correct |
2 |
Correct |
0 ms |
1960 KB |
Output is correct |
3 |
Correct |
0 ms |
1960 KB |
Output is correct |
4 |
Correct |
0 ms |
1960 KB |
Output is correct |
5 |
Correct |
0 ms |
1960 KB |
Output is correct |
6 |
Correct |
0 ms |
1960 KB |
Output is correct |
7 |
Correct |
0 ms |
1960 KB |
Output is correct |
8 |
Correct |
0 ms |
1960 KB |
Output is correct |
9 |
Correct |
0 ms |
1960 KB |
Output is correct |
10 |
Correct |
0 ms |
1960 KB |
Output is correct |
11 |
Correct |
0 ms |
1960 KB |
Output is correct |
12 |
Correct |
0 ms |
1960 KB |
Output is correct |
13 |
Correct |
0 ms |
1960 KB |
Output is correct |
14 |
Correct |
2 ms |
1960 KB |
Output is correct |
15 |
Correct |
2 ms |
1960 KB |
Output is correct |
16 |
Correct |
0 ms |
1960 KB |
Output is correct |
17 |
Correct |
6 ms |
1960 KB |
Output is correct |
18 |
Correct |
0 ms |
1960 KB |
Output is correct |
19 |
Correct |
0 ms |
1960 KB |
Output is correct |
20 |
Correct |
23 ms |
2092 KB |
Output is correct |
21 |
Correct |
0 ms |
1960 KB |
Output is correct |
22 |
Correct |
0 ms |
1960 KB |
Output is correct |
23 |
Correct |
8 ms |
1960 KB |
Output is correct |
24 |
Correct |
20 ms |
2092 KB |
Output is correct |
25 |
Correct |
2 ms |
2092 KB |
Output is correct |
26 |
Correct |
0 ms |
1960 KB |
Output is correct |
27 |
Correct |
0 ms |
1960 KB |
Output is correct |
28 |
Correct |
24 ms |
2092 KB |
Output is correct |
29 |
Correct |
12 ms |
2224 KB |
Output is correct |
30 |
Correct |
3 ms |
2144 KB |
Output is correct |
31 |
Correct |
9 ms |
2092 KB |
Output is correct |
32 |
Correct |
7 ms |
2152 KB |
Output is correct |
33 |
Correct |
29 ms |
2228 KB |
Output is correct |
34 |
Correct |
25 ms |
2232 KB |
Output is correct |
35 |
Correct |
186 ms |
2828 KB |
Output is correct |
36 |
Correct |
9 ms |
2096 KB |
Output is correct |
37 |
Correct |
143 ms |
2692 KB |
Output is correct |
38 |
Correct |
103 ms |
3072 KB |
Output is correct |
39 |
Correct |
10 ms |
2928 KB |
Output is correct |
40 |
Correct |
13 ms |
2928 KB |
Output is correct |
41 |
Correct |
48 ms |
3072 KB |
Output is correct |
42 |
Correct |
6 ms |
2844 KB |
Output is correct |
43 |
Correct |
6 ms |
2844 KB |
Output is correct |
44 |
Correct |
339 ms |
2840 KB |
Output is correct |
45 |
Correct |
393 ms |
3084 KB |
Output is correct |
46 |
Correct |
395 ms |
3080 KB |
Output is correct |
47 |
Correct |
720 ms |
3940 KB |
Output is correct |
48 |
Correct |
10 ms |
3432 KB |
Output is correct |
49 |
Correct |
11 ms |
3452 KB |
Output is correct |
50 |
Correct |
6 ms |
3228 KB |
Output is correct |
51 |
Execution timed out |
1000 ms |
5500 KB |
Program timed out |
52 |
Halted |
0 ms |
0 KB |
- |