This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ff first
#define ss second
#define PHB push_back
#define fastio ios::sync_with_stdio(false);cin.tie(nullptr);
#define ln '\n'
#define nwln cout<<ln;
#define v(T) vector<T>
#define PQ(T) priority_queue<T, v(T), greater<T>>
using namespace std;
typedef pair<int, int> pi;
typedef vector<int> vri;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
const int INF = 1001001001;
const int MAXN = 30003;
int N, M;
set<int> B[MAXN];
vri dist;
vvpi adj;
void dijkstra(int src) {
dist.assign(N, INF);
dist[src] = 0;
PQ(pi) pq;
pq.push({0, src});
while(!pq.empty()) {
int v = pq.top().ss, d_v = pq.top().ff;
pq.pop();
if(d_v != dist[v]) continue;
for(pi edge: adj[v]) {
int w = edge.ss, u = edge.ff;
if(dist[v] + w < dist[u]) {
dist[u] = dist[v] + w;
pq.push({dist[u], u});
}
}
}
}
int main()
{
fastio
cin>>N>>M, adj.resize(N);
while(M--) {
int b, p;
cin>>b>>p;
B[b].insert(p);
}
for(int i=0; i<N; ++i) {
for(int p: B[i]) {
int pos = i, cost = 0;
do {
pos += p, ++cost;
if(pos >= N) break;
adj[i].PHB({pos, cost});
} while(!B[pos].count(p));
}
}
for(int i=0; i<N; ++i) {
for(int p: B[i]) {
int pos = i, cost = 0;
do {
pos -= p, ++cost;
if(pos < 0) break;
adj[i].PHB({pos, cost});
} while(!B[pos].count(p));
}
}
dijkstra(0);
if(dist[1] == INF) cout<<-1;
else cout<<dist[1];
nwln
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |