#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define INF 1e9+7
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
using ll=long long;
using pii=pair<int,int>;
using ppi=pair<int,pii>;
using oset=tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>;
template<typename T>
void _print(vector<T> x) {cerr<<"{"; for(auto e:x) cerr<<e<<","; cerr<<"}";}
void _print(pii x) {cerr<<"{"<<x.first<<","<<x.second<<"}";}
template<typename T>
void _print(T x) {cerr<<x;}
void dbg() {cerr<<endl;}
template<typename Head,typename... Tail>
void dbg(Head H,Tail... T) {
_print(H);
if(sizeof...(T)) cerr<<",";
else cerr<<"\"]";
dbg(T...);
}
#define debug(...) cerr<<"["<<#__VA_ARGS__<<"]:[\"",dbg(__VA_ARGS__)
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int mxn=2010;
int n,m;
int adjm[mxn][mxn];
vector<pii> adj[mxn];
vector<int> a[mxn];
int s,t;
void dijk() {
priority_queue<pii,vector<pii>,greater<pii>> pq;
vector<int> dist(n,INF);
dist[s]=0;
pq.push({dist[s],s});
while(!pq.empty()) {
pii cur=pq.top(); pq.pop();
int u=cur.second,d=cur.first;
for(auto vw:adj[u]) {
int v=vw.first,w=vw.second;
if(d+w<dist[v]) {
dist[v]=d+w;
pq.push({dist[v],v});
}
}
}
if(dist[t]==INF) cout<<"-1"<<endl;
else cout<<dist[t]<<endl;
exit(0);
}
int main () {
ios::sync_with_stdio(false); cin.tie(0);
cin>>n>>m;
for(int i=0;i<m;i++) {
int u,w; cin>>u>>w;
if(!i) s=u;
else if(i==1) t=u;
a[u].push_back(w);
}
for(int i=0;i<n;i++) {
sort(all(a[i]));
a[i].erase(unique(all(a[i])),end(a[i]));
}
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
adjm[i][j]=INF;
}
}
for(int u=0;u<n;u++) {
for(int w:a[u]) {
for(int v=u-w,cnt=1;v>=0;v-=w,cnt++) {
adjm[u][v]=min(adjm[u][v],cnt);
}
for(int v=u+w,cnt=1;v<n;v+=w,cnt++) {
adjm[u][v]=min(adjm[u][v],cnt);
}
}
for(int v=0;v<n;v++) {
if(v==u) continue;
if(adjm[u][v]!=INF) adj[u].push_back({v,adjm[u][v]});
}
}
dijk();
}
# |
결과 |
실행 시간 |
메모리 |
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 |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
540 KB |
Output is correct |
10 |
Correct |
2 ms |
844 KB |
Output is correct |
11 |
Correct |
2 ms |
844 KB |
Output is correct |
12 |
Correct |
2 ms |
844 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
2 ms |
804 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 |
412 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 |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
844 KB |
Output is correct |
11 |
Correct |
2 ms |
844 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
2 ms |
844 KB |
Output is correct |
16 |
Correct |
2 ms |
1440 KB |
Output is correct |
17 |
Correct |
5 ms |
5836 KB |
Output is correct |
18 |
Correct |
14 ms |
14224 KB |
Output is correct |
19 |
Correct |
15 ms |
16204 KB |
Output is correct |
20 |
Correct |
83 ms |
48320 KB |
Output is correct |
21 |
Correct |
2 ms |
2600 KB |
Output is correct |
22 |
Correct |
14 ms |
14412 KB |
Output is correct |
23 |
Correct |
12 ms |
13104 KB |
Output is correct |
24 |
Correct |
16 ms |
15564 KB |
Output is correct |
25 |
Correct |
18 ms |
16296 KB |
Output is correct |
26 |
Correct |
16 ms |
16204 KB |
Output is correct |
27 |
Correct |
16 ms |
16228 KB |
Output is correct |
28 |
Correct |
17 ms |
16372 KB |
Output is correct |
29 |
Correct |
17 ms |
16596 KB |
Output is correct |
30 |
Correct |
16 ms |
16332 KB |
Output is correct |
31 |
Correct |
18 ms |
16580 KB |
Output is correct |
32 |
Correct |
16 ms |
16460 KB |
Output is correct |
33 |
Correct |
18 ms |
16768 KB |
Output is correct |
34 |
Correct |
18 ms |
16716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 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 |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
800 KB |
Output is correct |
11 |
Correct |
2 ms |
844 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
1 ms |
844 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
6 ms |
5836 KB |
Output is correct |
18 |
Correct |
14 ms |
14268 KB |
Output is correct |
19 |
Correct |
16 ms |
16196 KB |
Output is correct |
20 |
Correct |
81 ms |
48212 KB |
Output is correct |
21 |
Correct |
2 ms |
2596 KB |
Output is correct |
22 |
Correct |
15 ms |
14596 KB |
Output is correct |
23 |
Correct |
12 ms |
13128 KB |
Output is correct |
24 |
Correct |
17 ms |
15552 KB |
Output is correct |
25 |
Correct |
17 ms |
16236 KB |
Output is correct |
26 |
Correct |
16 ms |
16204 KB |
Output is correct |
27 |
Correct |
16 ms |
16192 KB |
Output is correct |
28 |
Correct |
17 ms |
16428 KB |
Output is correct |
29 |
Correct |
17 ms |
16596 KB |
Output is correct |
30 |
Correct |
16 ms |
16436 KB |
Output is correct |
31 |
Correct |
18 ms |
16480 KB |
Output is correct |
32 |
Correct |
21 ms |
16480 KB |
Output is correct |
33 |
Correct |
18 ms |
16716 KB |
Output is correct |
34 |
Correct |
18 ms |
16792 KB |
Output is correct |
35 |
Correct |
25 ms |
13632 KB |
Output is correct |
36 |
Correct |
9 ms |
9180 KB |
Output is correct |
37 |
Correct |
28 ms |
18932 KB |
Output is correct |
38 |
Correct |
31 ms |
18880 KB |
Output is correct |
39 |
Correct |
31 ms |
19108 KB |
Output is correct |
40 |
Correct |
30 ms |
18984 KB |
Output is correct |
41 |
Correct |
30 ms |
18824 KB |
Output is correct |
42 |
Correct |
22 ms |
16548 KB |
Output is correct |
43 |
Correct |
21 ms |
16484 KB |
Output is correct |
44 |
Correct |
83 ms |
48544 KB |
Output is correct |
45 |
Correct |
30 ms |
19012 KB |
Output is correct |
46 |
Correct |
28 ms |
19000 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 |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
844 KB |
Output is correct |
11 |
Correct |
2 ms |
844 KB |
Output is correct |
12 |
Correct |
1 ms |
844 KB |
Output is correct |
13 |
Correct |
2 ms |
972 KB |
Output is correct |
14 |
Correct |
2 ms |
844 KB |
Output is correct |
15 |
Correct |
2 ms |
844 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
5 ms |
5836 KB |
Output is correct |
18 |
Correct |
13 ms |
14156 KB |
Output is correct |
19 |
Correct |
16 ms |
16180 KB |
Output is correct |
20 |
Correct |
81 ms |
48220 KB |
Output is correct |
21 |
Correct |
3 ms |
2636 KB |
Output is correct |
22 |
Correct |
14 ms |
14496 KB |
Output is correct |
23 |
Correct |
12 ms |
13072 KB |
Output is correct |
24 |
Correct |
18 ms |
15564 KB |
Output is correct |
25 |
Correct |
20 ms |
16244 KB |
Output is correct |
26 |
Correct |
16 ms |
16312 KB |
Output is correct |
27 |
Correct |
16 ms |
16176 KB |
Output is correct |
28 |
Correct |
17 ms |
16356 KB |
Output is correct |
29 |
Correct |
22 ms |
16588 KB |
Output is correct |
30 |
Correct |
20 ms |
16440 KB |
Output is correct |
31 |
Correct |
17 ms |
16456 KB |
Output is correct |
32 |
Correct |
16 ms |
16460 KB |
Output is correct |
33 |
Correct |
18 ms |
16700 KB |
Output is correct |
34 |
Correct |
18 ms |
16784 KB |
Output is correct |
35 |
Correct |
22 ms |
13532 KB |
Output is correct |
36 |
Correct |
10 ms |
9256 KB |
Output is correct |
37 |
Correct |
29 ms |
18936 KB |
Output is correct |
38 |
Correct |
30 ms |
18868 KB |
Output is correct |
39 |
Correct |
30 ms |
19140 KB |
Output is correct |
40 |
Correct |
34 ms |
19008 KB |
Output is correct |
41 |
Correct |
38 ms |
18924 KB |
Output is correct |
42 |
Correct |
26 ms |
16588 KB |
Output is correct |
43 |
Correct |
21 ms |
16580 KB |
Output is correct |
44 |
Correct |
88 ms |
48576 KB |
Output is correct |
45 |
Correct |
27 ms |
18936 KB |
Output is correct |
46 |
Correct |
28 ms |
19104 KB |
Output is correct |
47 |
Runtime error |
43 ms |
33600 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |