#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define F first
#define S second
#define pb push_back
#define endl '\n'
#define Mp make_pair
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r+" , stdin) ; freopen("out.txt" , "w+" , stdout);
ll mod = 1e9+7 ;
const int N = 3e4+23;
const ll INF= 1e18+23;
vector<int> g[N];
int d[N];
int b[N];
int n , m;
ll dist[N];
int vis[N];
void bfs(){
fast_io
for (int i=0; i<=n; i++) dist[i]=INF;
dist[b[0]]=0;
priority_queue<pll, vector<pll>, greater<pll> > q;
q.push({0, b[0]});
while(!q.empty()){
int v=q.top().S; q.pop();
if(vis[v]) continue;
vis[v]=1;
for (int d: g[v]){
for (int i=1; v+i*d<n; i++){
int u=i*d+v;
if(dist[u]>dist[v]+i){
dist[u]=dist[v]+i;
q.push({dist[u], u});
}
}
for (int i=1; v-i*d>=0; i++){
int u=-i*d+v;
if(dist[u]>dist[v]+i){
dist[u]=dist[v]+i;
q.push({dist[u], u});
}
}
}
}
}
int main()
{
cin>>n>>m;
for (int i=0; i<m; i++) cin>>b[i]>>d[i];
for (int i=0; i<m; i++) g[b[i]].pb(d[i]);
bfs();
if(dist[b[1]]>=INF) dist[b[1]]=-1;
cout<<dist[b[1]]<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1112 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
0 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1112 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1008 KB |
Output is correct |
4 |
Correct |
0 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1112 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
2 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
5 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1368 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1368 KB |
Output is correct |
26 |
Correct |
5 ms |
1112 KB |
Output is correct |
27 |
Correct |
5 ms |
1112 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1500 KB |
Output is correct |
30 |
Correct |
1 ms |
1368 KB |
Output is correct |
31 |
Correct |
1 ms |
1368 KB |
Output is correct |
32 |
Correct |
1 ms |
1372 KB |
Output is correct |
33 |
Correct |
3 ms |
1628 KB |
Output is correct |
34 |
Correct |
3 ms |
1504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
0 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
0 ms |
1116 KB |
Output is correct |
7 |
Correct |
0 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1116 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1112 KB |
Output is correct |
15 |
Correct |
1 ms |
1112 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
5 ms |
1112 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
5 ms |
1116 KB |
Output is correct |
27 |
Correct |
4 ms |
1116 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1500 KB |
Output is correct |
30 |
Correct |
1 ms |
1372 KB |
Output is correct |
31 |
Correct |
1 ms |
1372 KB |
Output is correct |
32 |
Correct |
1 ms |
1368 KB |
Output is correct |
33 |
Correct |
3 ms |
1628 KB |
Output is correct |
34 |
Correct |
3 ms |
1556 KB |
Output is correct |
35 |
Correct |
10 ms |
1628 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
8 ms |
1696 KB |
Output is correct |
38 |
Correct |
12 ms |
1884 KB |
Output is correct |
39 |
Correct |
12 ms |
1628 KB |
Output is correct |
40 |
Correct |
12 ms |
1788 KB |
Output is correct |
41 |
Correct |
12 ms |
1884 KB |
Output is correct |
42 |
Correct |
64 ms |
1628 KB |
Output is correct |
43 |
Correct |
63 ms |
1624 KB |
Output is correct |
44 |
Correct |
65 ms |
1624 KB |
Output is correct |
45 |
Correct |
11 ms |
1884 KB |
Output is correct |
46 |
Correct |
11 ms |
1884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
0 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1112 KB |
Output is correct |
5 |
Correct |
0 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
1116 KB |
Output is correct |
7 |
Correct |
1 ms |
1116 KB |
Output is correct |
8 |
Correct |
1 ms |
1116 KB |
Output is correct |
9 |
Correct |
1 ms |
1112 KB |
Output is correct |
10 |
Correct |
1 ms |
1116 KB |
Output is correct |
11 |
Correct |
1 ms |
1116 KB |
Output is correct |
12 |
Correct |
1 ms |
1112 KB |
Output is correct |
13 |
Correct |
1 ms |
1116 KB |
Output is correct |
14 |
Correct |
1 ms |
1116 KB |
Output is correct |
15 |
Correct |
1 ms |
1116 KB |
Output is correct |
16 |
Correct |
1 ms |
1116 KB |
Output is correct |
17 |
Correct |
1 ms |
1116 KB |
Output is correct |
18 |
Correct |
1 ms |
1116 KB |
Output is correct |
19 |
Correct |
1 ms |
1116 KB |
Output is correct |
20 |
Correct |
5 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1116 KB |
Output is correct |
23 |
Correct |
1 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1116 KB |
Output is correct |
25 |
Correct |
2 ms |
1372 KB |
Output is correct |
26 |
Correct |
5 ms |
1116 KB |
Output is correct |
27 |
Correct |
4 ms |
1112 KB |
Output is correct |
28 |
Correct |
1 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
1500 KB |
Output is correct |
30 |
Correct |
1 ms |
1384 KB |
Output is correct |
31 |
Correct |
1 ms |
1384 KB |
Output is correct |
32 |
Correct |
1 ms |
1384 KB |
Output is correct |
33 |
Correct |
3 ms |
1640 KB |
Output is correct |
34 |
Correct |
3 ms |
1628 KB |
Output is correct |
35 |
Correct |
10 ms |
1628 KB |
Output is correct |
36 |
Correct |
2 ms |
1116 KB |
Output is correct |
37 |
Correct |
8 ms |
1628 KB |
Output is correct |
38 |
Correct |
12 ms |
1884 KB |
Output is correct |
39 |
Correct |
12 ms |
1784 KB |
Output is correct |
40 |
Correct |
12 ms |
1880 KB |
Output is correct |
41 |
Correct |
13 ms |
1880 KB |
Output is correct |
42 |
Correct |
74 ms |
1884 KB |
Output is correct |
43 |
Correct |
64 ms |
1624 KB |
Output is correct |
44 |
Correct |
75 ms |
1660 KB |
Output is correct |
45 |
Correct |
12 ms |
2040 KB |
Output is correct |
46 |
Correct |
11 ms |
1880 KB |
Output is correct |
47 |
Correct |
18 ms |
3028 KB |
Output is correct |
48 |
Correct |
10 ms |
1884 KB |
Output is correct |
49 |
Correct |
9 ms |
1884 KB |
Output is correct |
50 |
Correct |
7 ms |
1884 KB |
Output is correct |
51 |
Correct |
28 ms |
4300 KB |
Output is correct |
52 |
Correct |
26 ms |
3280 KB |
Output is correct |
53 |
Correct |
19 ms |
3280 KB |
Output is correct |
54 |
Correct |
2 ms |
1760 KB |
Output is correct |
55 |
Correct |
2 ms |
1760 KB |
Output is correct |
56 |
Correct |
820 ms |
3212 KB |
Output is correct |
57 |
Correct |
40 ms |
6604 KB |
Output is correct |
58 |
Correct |
757 ms |
2264 KB |
Output is correct |
59 |
Correct |
843 ms |
2264 KB |
Output is correct |
60 |
Correct |
769 ms |
2264 KB |
Output is correct |
61 |
Correct |
744 ms |
2264 KB |
Output is correct |
62 |
Correct |
15 ms |
2652 KB |
Output is correct |
63 |
Correct |
31 ms |
4052 KB |
Output is correct |
64 |
Correct |
36 ms |
6464 KB |
Output is correct |
65 |
Correct |
44 ms |
6868 KB |
Output is correct |
66 |
Correct |
51 ms |
6092 KB |
Output is correct |
67 |
Correct |
49 ms |
7300 KB |
Output is correct |