이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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+p, cost = 1;
while(pos < N) {
adj[i].PHB({pos, cost});
if(B[pos].count(p)) break;
++cost, pos += p;
}
pos = i-p, cost = 1;
while(pos >= 0) {
adj[i].PHB({pos, cost});
if(B[pos].count(p)) break;
++cost, pos -= 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... |