#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
using namespace std;
int n,m;
const int N=2009;
int vis[N][N],a[N][N],yes[N];
vector<pair<int,int>>v[N];
ll dis[N];
priority_queue<pair<ll,int>>q;
void go(int x,int y)
{ for(int i=x+y;i<n;i+=y)
{
if(vis[x][i]==1)continue;
vis[x][i]=1;
int z=i-x;z/=y;
v[x].pb({i,z});
}
for(int i=x-y;i>-1;i-=y)
{
if(vis[x][i]==1)continue;
vis[x][i]=1;
int z=x-i;z/=y;
v[x].pb({i,z});
}
}
int main()
{int ww=0;
cin>>n>>m;int w=0;
for(int i=0;i<m;i++)
{
int x,y;cin>>x>>y;
if(i==0)w=x;
if(i==1)ww=x;
a[x][y]=1;
}int MX=2009;
for(int i=0;i<n;i++)
{
for(int j=MX;j>-1;j--)
{
if(a[i][j]==0)continue;
go(i,j);
}
}
q.push({0,w});for(int i=0;i<n;i++)dis[i]=1e16;
while(!q.empty())
{
pair<int,int>x=q.top();
q.pop();
if(yes[x.se]==1)continue;
yes[x.se]=1;
dis[x.se]=-x.fi;
for(auto it:v[x.se])
{
if(yes[it.fi])continue;
if(dis[x.se]+it.se>=dis[it.fi])continue;
dis[it.fi]=dis[x.se]+it.se;
q.push({-dis[it.fi],it.fi});
}
}
if(yes[ww]==0)
{
cout<<-1;return 0;
}cout<<dis[ww];return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
2 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1256 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
868 KB |
Output is correct |
15 |
Correct |
2 ms |
872 KB |
Output is correct |
16 |
Correct |
3 ms |
2148 KB |
Output is correct |
17 |
Correct |
9 ms |
7884 KB |
Output is correct |
18 |
Correct |
11 ms |
7708 KB |
Output is correct |
19 |
Correct |
10 ms |
5068 KB |
Output is correct |
20 |
Correct |
99 ms |
56152 KB |
Output is correct |
21 |
Correct |
4 ms |
3276 KB |
Output is correct |
22 |
Correct |
10 ms |
5884 KB |
Output is correct |
23 |
Correct |
10 ms |
7012 KB |
Output is correct |
24 |
Correct |
16 ms |
13176 KB |
Output is correct |
25 |
Correct |
15 ms |
12108 KB |
Output is correct |
26 |
Correct |
9 ms |
1660 KB |
Output is correct |
27 |
Correct |
9 ms |
1228 KB |
Output is correct |
28 |
Correct |
17 ms |
15976 KB |
Output is correct |
29 |
Correct |
9 ms |
1432 KB |
Output is correct |
30 |
Correct |
8 ms |
988 KB |
Output is correct |
31 |
Correct |
9 ms |
1380 KB |
Output is correct |
32 |
Correct |
9 ms |
1228 KB |
Output is correct |
33 |
Correct |
10 ms |
1884 KB |
Output is correct |
34 |
Correct |
10 ms |
1760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
3 ms |
844 KB |
Output is correct |
16 |
Correct |
3 ms |
2124 KB |
Output is correct |
17 |
Correct |
9 ms |
7944 KB |
Output is correct |
18 |
Correct |
11 ms |
7640 KB |
Output is correct |
19 |
Correct |
10 ms |
5060 KB |
Output is correct |
20 |
Correct |
88 ms |
56260 KB |
Output is correct |
21 |
Correct |
4 ms |
3276 KB |
Output is correct |
22 |
Correct |
10 ms |
5960 KB |
Output is correct |
23 |
Correct |
10 ms |
6988 KB |
Output is correct |
24 |
Correct |
16 ms |
13256 KB |
Output is correct |
25 |
Correct |
15 ms |
12204 KB |
Output is correct |
26 |
Correct |
9 ms |
1716 KB |
Output is correct |
27 |
Correct |
9 ms |
1260 KB |
Output is correct |
28 |
Correct |
17 ms |
16000 KB |
Output is correct |
29 |
Correct |
9 ms |
1432 KB |
Output is correct |
30 |
Correct |
8 ms |
1004 KB |
Output is correct |
31 |
Correct |
9 ms |
1372 KB |
Output is correct |
32 |
Correct |
9 ms |
1316 KB |
Output is correct |
33 |
Correct |
10 ms |
1768 KB |
Output is correct |
34 |
Correct |
10 ms |
1892 KB |
Output is correct |
35 |
Correct |
38 ms |
24516 KB |
Output is correct |
36 |
Correct |
14 ms |
14156 KB |
Output is correct |
37 |
Correct |
41 ms |
32068 KB |
Output is correct |
38 |
Correct |
49 ms |
34448 KB |
Output is correct |
39 |
Correct |
48 ms |
34684 KB |
Output is correct |
40 |
Correct |
48 ms |
34556 KB |
Output is correct |
41 |
Correct |
47 ms |
34356 KB |
Output is correct |
42 |
Correct |
25 ms |
1884 KB |
Output is correct |
43 |
Correct |
22 ms |
1484 KB |
Output is correct |
44 |
Correct |
102 ms |
56472 KB |
Output is correct |
45 |
Correct |
27 ms |
6108 KB |
Output is correct |
46 |
Correct |
27 ms |
6148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
2 ms |
1100 KB |
Output is correct |
11 |
Correct |
3 ms |
1228 KB |
Output is correct |
12 |
Correct |
2 ms |
332 KB |
Output is correct |
13 |
Correct |
3 ms |
1228 KB |
Output is correct |
14 |
Correct |
2 ms |
868 KB |
Output is correct |
15 |
Correct |
2 ms |
844 KB |
Output is correct |
16 |
Correct |
3 ms |
2124 KB |
Output is correct |
17 |
Correct |
10 ms |
7888 KB |
Output is correct |
18 |
Correct |
11 ms |
7652 KB |
Output is correct |
19 |
Correct |
10 ms |
5068 KB |
Output is correct |
20 |
Correct |
94 ms |
56272 KB |
Output is correct |
21 |
Correct |
4 ms |
3276 KB |
Output is correct |
22 |
Correct |
10 ms |
5848 KB |
Output is correct |
23 |
Correct |
10 ms |
7028 KB |
Output is correct |
24 |
Correct |
15 ms |
13172 KB |
Output is correct |
25 |
Correct |
15 ms |
12204 KB |
Output is correct |
26 |
Correct |
9 ms |
1612 KB |
Output is correct |
27 |
Correct |
9 ms |
1228 KB |
Output is correct |
28 |
Correct |
18 ms |
16204 KB |
Output is correct |
29 |
Correct |
9 ms |
1356 KB |
Output is correct |
30 |
Correct |
8 ms |
1048 KB |
Output is correct |
31 |
Correct |
9 ms |
1380 KB |
Output is correct |
32 |
Correct |
8 ms |
1228 KB |
Output is correct |
33 |
Correct |
10 ms |
1776 KB |
Output is correct |
34 |
Correct |
10 ms |
1896 KB |
Output is correct |
35 |
Correct |
40 ms |
24620 KB |
Output is correct |
36 |
Correct |
14 ms |
14156 KB |
Output is correct |
37 |
Correct |
44 ms |
32032 KB |
Output is correct |
38 |
Correct |
50 ms |
34364 KB |
Output is correct |
39 |
Correct |
49 ms |
34664 KB |
Output is correct |
40 |
Correct |
51 ms |
34480 KB |
Output is correct |
41 |
Correct |
49 ms |
34392 KB |
Output is correct |
42 |
Correct |
22 ms |
1808 KB |
Output is correct |
43 |
Correct |
22 ms |
1484 KB |
Output is correct |
44 |
Correct |
104 ms |
56452 KB |
Output is correct |
45 |
Correct |
27 ms |
6208 KB |
Output is correct |
46 |
Correct |
28 ms |
6220 KB |
Output is correct |
47 |
Runtime error |
3 ms |
460 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |