이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int nax = 30005;
const int INF = 1e9 + 5;
vector<pii> adj[nax];
vector<int> dist(nax, INF);
set<int> used[nax];
set<int> all[nax];
struct Edge {
int to, w;
bool operator<(const Edge& other) const {
return w > other.w;
}
};
priority_queue<Edge> q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<pii> a(m);
for(auto& i : a) {
cin >> i.first >> i.second;
}
for(auto i : a) {
all[i.first].insert(i.second);
}
for(auto i : a) {
if (used[i.first].count(i.second)) continue;
used[i.first].insert(i.second);
int idx = i.first + i.second;
for(int cost = 1;;cost++) {
if (idx >= n) break;
adj[i.first].push_back({idx, cost});
if (all[idx].count(i.second)) break;
// cout << i.first << "->" << idx << " " << cost << "\n";
idx += i.second;
}
idx = i.first - i.second;
for(int cost = 1;;cost++) {
if (idx < 0) break;
adj[i.first].push_back({idx, cost});
if (all[idx].count(i.second)) break;
// cout << i.first << "->" << idx << " " << cost << "\n";
idx -= i.second;
}
}
q.push({a[0].first, 0});
dist[a[0].first] = 0;
while(!q.empty()) {
Edge v = q.top();
q.pop();
if (dist[v.to] < v.w) continue;
for(pii u : adj[v.to]) {
if (dist[u.first] > u.second + v.w) {
dist[u.first] = u.second + v.w;
q.push({u.first, dist[u.first]});
}
}
}
cout << (dist[a[1].first] == INF ? -1 : dist[a[1].first]) << "\n";
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... |