#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define debug(x) cout << "Line " << __LINE__ << ", " << #x << " is " << x << endl
#define all(x) x.begin(), x.end()
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define ull unsigned long long
#define pll pair<ll, ll>
#define pii pair<ll, ll>
#define ld long double
#define nl endl
#define tb '\t'
#define sp ' '
#define sqr(x) (x)*(x)
#define arr3 array<int, 3>
using namespace std;
const int MX=30002, MOD=1000000007, BLOCK=327, INF=2e9+7, LG=62;
const ll INFF=1000000000000000007;
const ld ERR=1e-6, pi=3.14159265358979323846;
int N, M, B[MX], P[MX];
map<int, int> vis[MX];
vector<int> doge[MX];
int main(){
fastio;
cin >> N >> M;
for(int i=0; i<M; i++){
cin >> B[i] >> P[i];
doge[B[i]].pb(P[i]);
}
priority_queue<arr3, vector<arr3>, greater<arr3>> PQ;
PQ.push({0, B[0], P[0]});
vis[B[0]][P[0]]=0;
while(PQ.size()){
int cost=PQ.top()[0], idx=PQ.top()[1], p=PQ.top()[2];
PQ.pop();
if(vis[idx].count(p) && vis[idx][p]<cost)
continue;
for(int i:doge[idx]){
if(vis[idx].count(i) && vis[idx][i]<cost)
continue;
vis[idx][i]=cost;
for(int j=1, jj; idx+i*j<=N; j++){
jj=idx+i*j;
if(!vis[jj].count(i) || vis[jj][i]>cost+j){
vis[jj][i]=cost+j;
PQ.push({cost+j, jj});
}
}
for(int j=1, jj; idx-i*j>0; j++){
jj=idx-i*j;
if(!vis[jj].count(i) || vis[jj][i]>cost+j){
vis[jj][i]=cost+j;
PQ.push({cost+j, jj});
}
}
}
}
int ans=INF;
for(int i=0; i<=30000; i++){
if(vis[B[1]].count(i))
ans=min(ans, vis[B[1]][i]);
}
if(ans==INF)
ans=-1;
cout << ans << nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
3 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
1 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
1 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2436 KB |
Output is correct |
6 |
Correct |
2 ms |
2440 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
3 ms |
2516 KB |
Output is correct |
11 |
Correct |
8 ms |
2644 KB |
Output is correct |
12 |
Correct |
6 ms |
2388 KB |
Output is correct |
13 |
Correct |
3 ms |
2388 KB |
Output is correct |
14 |
Correct |
6 ms |
2772 KB |
Output is correct |
15 |
Correct |
6 ms |
2700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2440 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2440 KB |
Output is correct |
6 |
Correct |
3 ms |
2440 KB |
Output is correct |
7 |
Correct |
2 ms |
2440 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
3 ms |
2388 KB |
Output is correct |
10 |
Correct |
3 ms |
2440 KB |
Output is correct |
11 |
Correct |
12 ms |
2732 KB |
Output is correct |
12 |
Correct |
4 ms |
2388 KB |
Output is correct |
13 |
Correct |
2 ms |
2388 KB |
Output is correct |
14 |
Correct |
5 ms |
2772 KB |
Output is correct |
15 |
Correct |
5 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2388 KB |
Output is correct |
17 |
Correct |
11 ms |
3284 KB |
Output is correct |
18 |
Correct |
2 ms |
2456 KB |
Output is correct |
19 |
Correct |
2 ms |
2444 KB |
Output is correct |
20 |
Correct |
20 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
3 ms |
2388 KB |
Output is correct |
23 |
Correct |
4 ms |
2772 KB |
Output is correct |
24 |
Correct |
7 ms |
3156 KB |
Output is correct |
25 |
Correct |
4 ms |
2736 KB |
Output is correct |
26 |
Correct |
35 ms |
2772 KB |
Output is correct |
27 |
Correct |
43 ms |
2836 KB |
Output is correct |
28 |
Incorrect |
6 ms |
3476 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2448 KB |
Output is correct |
2 |
Correct |
2 ms |
2388 KB |
Output is correct |
3 |
Correct |
2 ms |
2388 KB |
Output is correct |
4 |
Correct |
2 ms |
2388 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2388 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2388 KB |
Output is correct |
10 |
Correct |
2 ms |
2516 KB |
Output is correct |
11 |
Correct |
9 ms |
2736 KB |
Output is correct |
12 |
Correct |
4 ms |
2388 KB |
Output is correct |
13 |
Correct |
3 ms |
2388 KB |
Output is correct |
14 |
Correct |
5 ms |
2704 KB |
Output is correct |
15 |
Correct |
8 ms |
2704 KB |
Output is correct |
16 |
Correct |
3 ms |
2388 KB |
Output is correct |
17 |
Correct |
14 ms |
3220 KB |
Output is correct |
18 |
Correct |
2 ms |
2448 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
23 ms |
2584 KB |
Output is correct |
21 |
Correct |
2 ms |
2452 KB |
Output is correct |
22 |
Correct |
2 ms |
2388 KB |
Output is correct |
23 |
Correct |
4 ms |
2772 KB |
Output is correct |
24 |
Correct |
6 ms |
3092 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
37 ms |
2860 KB |
Output is correct |
27 |
Correct |
43 ms |
2772 KB |
Output is correct |
28 |
Incorrect |
6 ms |
3412 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2388 KB |
Output is correct |
2 |
Correct |
2 ms |
2324 KB |
Output is correct |
3 |
Correct |
3 ms |
2388 KB |
Output is correct |
4 |
Correct |
3 ms |
2440 KB |
Output is correct |
5 |
Correct |
2 ms |
2388 KB |
Output is correct |
6 |
Correct |
2 ms |
2516 KB |
Output is correct |
7 |
Correct |
2 ms |
2388 KB |
Output is correct |
8 |
Correct |
2 ms |
2388 KB |
Output is correct |
9 |
Correct |
2 ms |
2436 KB |
Output is correct |
10 |
Correct |
3 ms |
2516 KB |
Output is correct |
11 |
Correct |
8 ms |
2644 KB |
Output is correct |
12 |
Correct |
3 ms |
2388 KB |
Output is correct |
13 |
Correct |
3 ms |
2388 KB |
Output is correct |
14 |
Correct |
5 ms |
2772 KB |
Output is correct |
15 |
Correct |
5 ms |
2772 KB |
Output is correct |
16 |
Correct |
2 ms |
2388 KB |
Output is correct |
17 |
Correct |
10 ms |
3284 KB |
Output is correct |
18 |
Correct |
2 ms |
2388 KB |
Output is correct |
19 |
Correct |
2 ms |
2388 KB |
Output is correct |
20 |
Correct |
28 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2388 KB |
Output is correct |
22 |
Correct |
2 ms |
2388 KB |
Output is correct |
23 |
Correct |
5 ms |
2704 KB |
Output is correct |
24 |
Correct |
6 ms |
3156 KB |
Output is correct |
25 |
Correct |
3 ms |
2864 KB |
Output is correct |
26 |
Correct |
35 ms |
2840 KB |
Output is correct |
27 |
Correct |
42 ms |
2812 KB |
Output is correct |
28 |
Incorrect |
7 ms |
3412 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |