#include <bits/stdc++.h>
using namespace std;
short int n,p[30004],b[30004],m;
priority_queue <pair<short int,short int> ,vector <pair<short int,short int> >,greater <pair<short int,short int> > > pq;
bool vis[30004];
set <int> v[30004];
set <int>::iterator it;
short int cur[30004];
void dij()
{
for (int i=0;i<n;i++)
cur[i]=32000;
cur[b[0]]=0;
pq.push({0,b[0]});
while (!pq.empty())
{
short int x= (pq.top()).second;
short int y= (pq.top()).first;
pq.pop();
if (vis[x])
continue;
vis[x]=true;
if (x==b[1])
return;
for (it=v[x].begin();it!=v[x].end();it++)
{
int c= (*it);
for (int j=1;j*c+x<n||(x-j*c)>=0;j++)
{
int a= (j*c+x);
if (a<n)
{
if (!vis[a]&&cur[a]>y+j)
{
cur[a]=y+j;
pq.push({cur[a],a});
}
}
a= (x-j*c);
if (a>=0)
{
if (!vis[a]&&cur[a]>y+j)
{
cur[a]=y+j;
pq.push({cur[a],a});
}
}
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >>n>>m;
for (int i=0;i<m;i++)
{
cin >>b[i]>>p[i];
v[b[i]].insert(p[i]);
}
dij();
if (!vis[b[1]])
cout <<-1;
else
cout <<cur[b[1]];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1784 KB |
Output is correct |
2 |
Correct |
2 ms |
1784 KB |
Output is correct |
3 |
Correct |
3 ms |
1964 KB |
Output is correct |
4 |
Correct |
2 ms |
1964 KB |
Output is correct |
5 |
Correct |
2 ms |
1964 KB |
Output is correct |
6 |
Correct |
3 ms |
1964 KB |
Output is correct |
7 |
Correct |
2 ms |
2004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2004 KB |
Output is correct |
2 |
Correct |
3 ms |
2004 KB |
Output is correct |
3 |
Correct |
3 ms |
2012 KB |
Output is correct |
4 |
Correct |
3 ms |
2012 KB |
Output is correct |
5 |
Correct |
3 ms |
2016 KB |
Output is correct |
6 |
Correct |
3 ms |
2016 KB |
Output is correct |
7 |
Correct |
3 ms |
2016 KB |
Output is correct |
8 |
Correct |
2 ms |
2016 KB |
Output is correct |
9 |
Correct |
2 ms |
2016 KB |
Output is correct |
10 |
Correct |
3 ms |
2016 KB |
Output is correct |
11 |
Correct |
3 ms |
2016 KB |
Output is correct |
12 |
Correct |
3 ms |
2016 KB |
Output is correct |
13 |
Correct |
3 ms |
2016 KB |
Output is correct |
14 |
Correct |
3 ms |
2140 KB |
Output is correct |
15 |
Correct |
3 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2140 KB |
Output is correct |
2 |
Correct |
3 ms |
2140 KB |
Output is correct |
3 |
Correct |
2 ms |
2140 KB |
Output is correct |
4 |
Correct |
2 ms |
2140 KB |
Output is correct |
5 |
Correct |
3 ms |
2140 KB |
Output is correct |
6 |
Correct |
3 ms |
2156 KB |
Output is correct |
7 |
Correct |
4 ms |
2156 KB |
Output is correct |
8 |
Correct |
3 ms |
2156 KB |
Output is correct |
9 |
Correct |
3 ms |
2156 KB |
Output is correct |
10 |
Correct |
2 ms |
2156 KB |
Output is correct |
11 |
Correct |
3 ms |
2156 KB |
Output is correct |
12 |
Correct |
3 ms |
2156 KB |
Output is correct |
13 |
Correct |
3 ms |
2156 KB |
Output is correct |
14 |
Correct |
3 ms |
2156 KB |
Output is correct |
15 |
Correct |
3 ms |
2156 KB |
Output is correct |
16 |
Correct |
3 ms |
2156 KB |
Output is correct |
17 |
Correct |
3 ms |
2156 KB |
Output is correct |
18 |
Correct |
4 ms |
2156 KB |
Output is correct |
19 |
Correct |
7 ms |
2156 KB |
Output is correct |
20 |
Correct |
12 ms |
2160 KB |
Output is correct |
21 |
Correct |
3 ms |
2160 KB |
Output is correct |
22 |
Correct |
3 ms |
2160 KB |
Output is correct |
23 |
Correct |
3 ms |
2160 KB |
Output is correct |
24 |
Correct |
3 ms |
2160 KB |
Output is correct |
25 |
Correct |
3 ms |
2160 KB |
Output is correct |
26 |
Correct |
3 ms |
2160 KB |
Output is correct |
27 |
Correct |
3 ms |
2160 KB |
Output is correct |
28 |
Correct |
3 ms |
2160 KB |
Output is correct |
29 |
Correct |
5 ms |
2160 KB |
Output is correct |
30 |
Correct |
3 ms |
2160 KB |
Output is correct |
31 |
Correct |
4 ms |
2160 KB |
Output is correct |
32 |
Correct |
4 ms |
2160 KB |
Output is correct |
33 |
Correct |
7 ms |
2296 KB |
Output is correct |
34 |
Correct |
6 ms |
2296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2296 KB |
Output is correct |
2 |
Correct |
3 ms |
2296 KB |
Output is correct |
3 |
Correct |
3 ms |
2296 KB |
Output is correct |
4 |
Correct |
3 ms |
2296 KB |
Output is correct |
5 |
Correct |
3 ms |
2296 KB |
Output is correct |
6 |
Correct |
3 ms |
2296 KB |
Output is correct |
7 |
Correct |
3 ms |
2296 KB |
Output is correct |
8 |
Correct |
3 ms |
2296 KB |
Output is correct |
9 |
Correct |
3 ms |
2296 KB |
Output is correct |
10 |
Correct |
3 ms |
2296 KB |
Output is correct |
11 |
Correct |
3 ms |
2296 KB |
Output is correct |
12 |
Correct |
3 ms |
2296 KB |
Output is correct |
13 |
Correct |
3 ms |
2296 KB |
Output is correct |
14 |
Correct |
3 ms |
2296 KB |
Output is correct |
15 |
Correct |
3 ms |
2296 KB |
Output is correct |
16 |
Correct |
3 ms |
2296 KB |
Output is correct |
17 |
Correct |
3 ms |
2296 KB |
Output is correct |
18 |
Correct |
3 ms |
2296 KB |
Output is correct |
19 |
Correct |
3 ms |
2296 KB |
Output is correct |
20 |
Correct |
10 ms |
2296 KB |
Output is correct |
21 |
Correct |
3 ms |
2296 KB |
Output is correct |
22 |
Correct |
3 ms |
2296 KB |
Output is correct |
23 |
Correct |
3 ms |
2296 KB |
Output is correct |
24 |
Correct |
3 ms |
2296 KB |
Output is correct |
25 |
Correct |
3 ms |
2296 KB |
Output is correct |
26 |
Correct |
4 ms |
2296 KB |
Output is correct |
27 |
Correct |
3 ms |
2296 KB |
Output is correct |
28 |
Correct |
3 ms |
2296 KB |
Output is correct |
29 |
Correct |
5 ms |
2296 KB |
Output is correct |
30 |
Correct |
4 ms |
2296 KB |
Output is correct |
31 |
Correct |
4 ms |
2296 KB |
Output is correct |
32 |
Correct |
4 ms |
2296 KB |
Output is correct |
33 |
Correct |
6 ms |
2296 KB |
Output is correct |
34 |
Correct |
5 ms |
2296 KB |
Output is correct |
35 |
Correct |
16 ms |
3436 KB |
Output is correct |
36 |
Correct |
4 ms |
3436 KB |
Output is correct |
37 |
Correct |
13 ms |
3436 KB |
Output is correct |
38 |
Correct |
17 ms |
3564 KB |
Output is correct |
39 |
Correct |
15 ms |
3564 KB |
Output is correct |
40 |
Correct |
14 ms |
3564 KB |
Output is correct |
41 |
Correct |
16 ms |
3664 KB |
Output is correct |
42 |
Correct |
7 ms |
3664 KB |
Output is correct |
43 |
Correct |
8 ms |
3664 KB |
Output is correct |
44 |
Correct |
14 ms |
3664 KB |
Output is correct |
45 |
Correct |
22 ms |
3820 KB |
Output is correct |
46 |
Correct |
30 ms |
3820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3820 KB |
Output is correct |
2 |
Correct |
3 ms |
3820 KB |
Output is correct |
3 |
Correct |
2 ms |
3820 KB |
Output is correct |
4 |
Correct |
3 ms |
3820 KB |
Output is correct |
5 |
Correct |
3 ms |
3820 KB |
Output is correct |
6 |
Correct |
2 ms |
3820 KB |
Output is correct |
7 |
Correct |
3 ms |
3820 KB |
Output is correct |
8 |
Correct |
3 ms |
3820 KB |
Output is correct |
9 |
Correct |
3 ms |
3820 KB |
Output is correct |
10 |
Correct |
3 ms |
3820 KB |
Output is correct |
11 |
Correct |
3 ms |
3820 KB |
Output is correct |
12 |
Correct |
3 ms |
3820 KB |
Output is correct |
13 |
Correct |
4 ms |
3820 KB |
Output is correct |
14 |
Correct |
3 ms |
3820 KB |
Output is correct |
15 |
Correct |
3 ms |
3820 KB |
Output is correct |
16 |
Correct |
3 ms |
3820 KB |
Output is correct |
17 |
Correct |
4 ms |
3820 KB |
Output is correct |
18 |
Correct |
3 ms |
3820 KB |
Output is correct |
19 |
Correct |
2 ms |
3820 KB |
Output is correct |
20 |
Correct |
9 ms |
3820 KB |
Output is correct |
21 |
Correct |
3 ms |
3820 KB |
Output is correct |
22 |
Correct |
3 ms |
3820 KB |
Output is correct |
23 |
Correct |
3 ms |
3820 KB |
Output is correct |
24 |
Correct |
4 ms |
3820 KB |
Output is correct |
25 |
Correct |
4 ms |
3820 KB |
Output is correct |
26 |
Correct |
3 ms |
3820 KB |
Output is correct |
27 |
Correct |
4 ms |
3820 KB |
Output is correct |
28 |
Correct |
3 ms |
3820 KB |
Output is correct |
29 |
Correct |
5 ms |
3820 KB |
Output is correct |
30 |
Correct |
4 ms |
3820 KB |
Output is correct |
31 |
Correct |
4 ms |
3820 KB |
Output is correct |
32 |
Correct |
4 ms |
3820 KB |
Output is correct |
33 |
Correct |
7 ms |
3820 KB |
Output is correct |
34 |
Correct |
6 ms |
3820 KB |
Output is correct |
35 |
Correct |
17 ms |
3820 KB |
Output is correct |
36 |
Correct |
4 ms |
3820 KB |
Output is correct |
37 |
Correct |
17 ms |
3820 KB |
Output is correct |
38 |
Correct |
24 ms |
3820 KB |
Output is correct |
39 |
Correct |
16 ms |
3820 KB |
Output is correct |
40 |
Correct |
15 ms |
3820 KB |
Output is correct |
41 |
Correct |
20 ms |
3820 KB |
Output is correct |
42 |
Correct |
7 ms |
3820 KB |
Output is correct |
43 |
Correct |
8 ms |
3820 KB |
Output is correct |
44 |
Correct |
21 ms |
3820 KB |
Output is correct |
45 |
Correct |
22 ms |
3820 KB |
Output is correct |
46 |
Correct |
19 ms |
3820 KB |
Output is correct |
47 |
Correct |
17 ms |
3820 KB |
Output is correct |
48 |
Correct |
10 ms |
3820 KB |
Output is correct |
49 |
Correct |
15 ms |
3820 KB |
Output is correct |
50 |
Correct |
9 ms |
3820 KB |
Output is correct |
51 |
Correct |
23 ms |
4204 KB |
Output is correct |
52 |
Correct |
24 ms |
4204 KB |
Output is correct |
53 |
Correct |
14 ms |
4204 KB |
Output is correct |
54 |
Correct |
4 ms |
4204 KB |
Output is correct |
55 |
Correct |
6 ms |
4204 KB |
Output is correct |
56 |
Execution timed out |
1071 ms |
4204 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |