이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;
const int N = 3e4 + 5;
const int oo = 1e18 + 7, mod = 1e9 + 7;
int n, m;
int b[N], p[N];
bool vis[N][205];
int mn_dist[N];
vector<int> doges[N];
priority_queue<pair<int, ii>, vector<pair<int, ii>>, greater<pair<int, ii>>> pq;
bool beg[N];
void process(){
cin >> n >> m;
for(int i = 1; i <= m; i++){
cin >> b[i] >> p[i];
b[i]++;
doges[b[i]].pb(p[i]);
}
for(int i = 1; i <= n; i++) mn_dist[i] = oo;
pq.push({0, {b[1], p[1]}});
mn_dist[b[1]] = 0;
if(p[1] <= 200) vis[b[1]][p[1]] = 1;
while(!pq.empty()){
int d = pq.top().fi, u = pq.top().se.fi, p = pq.top().se.se;
pq.pop();
if(p <= 200){
if((u > p) && !vis[u - p][p]){
vis[u - p][p] = 1;
mn_dist[u - p] = min(mn_dist[u - p], d + 1);
pq.push({d + 1, {u - p, p}});
}
if((u + p) <= n && !vis[u + p][p]){
vis[u + p][p] = 1;
mn_dist[u + p] = min(mn_dist[u + p], d + 1);
pq.push({d + 1, {u + p, p}});
}
}
if(d == mn_dist[u] && !beg[u]){
beg[u] = 1;
for(auto it : doges[u]){
if(it <= 200){
vis[u][it] = 1;
pq.push({d, {u, it}});
}
else{
for(int i = (u % it); i <= n; i += it){
if(!i) continue;
int dist = abs(i - u) / it;
if(mn_dist[i] > (mn_dist[u] + dist)){
mn_dist[i] = mn_dist[u] + dist;
pq.push({mn_dist[i], {u, it}});
}
}
}
}
}
}
cout << (mn_dist[b[2]] == oo ? -1 : mn_dist[b[2]]) << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
//freopen("doge.inp", "r", stdin);
//freopen("doge.out", "w", stdout);
cin.tie(0);
process();
}
# | 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... |