이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int MOD = (int)1e9 + 7;
const int NS = (int)3e4 + 4;
int N, M;
int B[NS], P[NS];
vector<pair<int, int>> way[NS];
int far[NS], chk[NS];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
map<int, int> cnt[NS];
vector<int> put[NS];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for(int i = 0; i < M; ++i){
cin >> B[i] >> P[i];
if(i == 1 && B[0] == B[1]){
cout << 0;
return 0;
}
if(cnt[B[i]][P[i]]){
--i, --M;
continue;
}
cnt[B[i]][P[i]] = i + 1;
put[B[i]].push_back(i);
}
for(int i = 0; i < M; ++i){
int now = B[i], mov = 0;
while(now < N){
if(now > B[i] && cnt[now][P[i]]){
way[i].push_back({cnt[now][P[i]] - 1, mov});
break;
}
for(auto&in:put[now]){
way[i].push_back({in, mov});
}
now += P[i], ++mov;
}
now = B[i] - P[i], mov = 1;
while(now >= 0){
if(cnt[now][P[i]]){
way[i].push_back({cnt[now][P[i]] - 1, mov});
break;
}
for(auto&in:put[now]){
way[i].push_back({in, mov});
}
now -= P[i], ++mov;
}
}
pq.push({0, 0});
for(int i = 0; i < M; ++i){
far[i] = MOD;
}
far[0] = 0;
while(!pq.empty()){
while(!pq.empty() && chk[pq.top().second]){
pq.pop();
}
if(pq.empty()){
break;
}
auto top = pq.top(); pq.pop();
chk[top.second] = 1;
for(auto&nxt:way[top.second]){
if(far[top.second] + nxt.second < far[nxt.first]){
far[nxt.first] = far[top.second] + nxt.second;
pq.push({far[nxt.first], nxt.first});
}
}
}
if(far[1] == MOD){
cout << -1;
}
else{
cout << far[1];
}
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... |