이 제출은 이전 버전의 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);
for(int i = 0 ; i < m ; i++){
int a , b;
cin >> a >> b;
for(int j = a + b , cnt = 1 ; j < n ; j += b , cnt++){
adj[a].emplace_back(j , cnt);
}
for(int j = a - b , cnt = 1 ; j >= 0 ; j -= b , cnt++){
adj[a].emplace_back(j , cnt);
}
}
priority_queue<pair<int,int> , vector<pair<int,int>> , greater<pair<int,int>>> pq;
pq.emplace(0 , 0);
vector<int> dist(n + 1 , (int)1e9);
dist[0] = 0;
while(!pq.empty()){
auto f = pq.top();
pq.pop();
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[1] == (int)1e9) cout << -1 << '\n';
else cout << dist[1] << '\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... |