이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n , m;
cin >> n >> m;
vector<vector<pair<int,int>>> adj(n + 1);
const int N = 30000 + 10;
int s = -1 , d = -1;
set<int> st[N];
for(int i = 0 ; i < m ; i++){
int a , b;
cin >> a >> b;
if(s == -1) s = a;
else if(d == -1) d = a;
st[a].insert(b);
}
for(int i = 0 ; i < n ; i++){
for(auto x : st[i]){
for(int j = i + x , cnt = 1 ; j < n ; j += x , cnt++){
adj[i].emplace_back(j , cnt);
if(st[j].find(x) != st[j].end()) break;
}
for(int j = i - x , cnt = 1 ; j >= 0 ; j -= x , cnt++){
adj[i].emplace_back(j , cnt);
if(st[j].find(x) != st[j].end()) break;
}
}
}
for(int i = 0 ; i < n ; i++) st[i].clear();
priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>> pq;
pq.emplace(0 , s);
vector<int> dist(n + 1 , (int)1e9);
dist[s] = 0;
while(!pq.empty()){
auto f = pq.top();
pq.pop();
if(f.second == d) break;
if(f.first != dist[f.second]) continue;
for(auto x : adj[f.second]){
int node = x.first;
int cost = x.second;
if(dist[node] > f.first + cost){
dist[node] = f.first + cost;
pq.emplace(dist[node] , node);
}
}
}
if(dist[d] == (int)1e9) cout << -1 << '\n';
else cout << dist[d] << '\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... |