이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int MAX = (3e4)+1, INF = 1e9;
int n, m, bc, sz, d[MAX];
vector<int> a[MAX];
vector<pair<int,int> > g[MAX];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
bc = sqrt(n), sz = n/bc;
for(int i = 0, pos, len; i < m; i++) {
cin >> pos >> len;
a[pos].push_back(len);
}
for(int i = 0; i < n; i++) for(int len: a[i]) {
for(int j = i-len; j >= 0; j -= len) g[i].emplace_back(j, (i-j)/len);
for(int j = i+len; j < n; j += len) g[i].emplace_back(j, (j-i)/len);
}
fill(d+1, d+n, INF);
priority_queue<pair<int,int> > pq;
pq.emplace(0, 0);
while(!pq.empty()) {
int u = pq.top().second;
pq.pop();
for(auto p: g[u]) {
int v, w; tie(v, w) = p;
if(d[u] + w < d[v]) {
d[v] = d[u] + w;
pq.emplace(-d[v], v);
}
}
}
cout << (d[1] == INF ? -1 : d[1]);
}
# | 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... |