이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize (03)
#define int long long
#define pb push_back
#define tii tuple<int,int,int>
#define pii pair<int,int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
const int maxn = 3e4 + 5, INF = 0x3f3f3f3f;
vector<int> doges[maxn];
int dist[maxn][300];
priority_queue<tii, vector<tii>, greater<tii>> pq;
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0);
int n, m; cin >> n >> m;
pii doge0, doge1; cin >> doge0.ff >> doge0.ss >> doge1.ff >> doge1.ss;
doges[doge0.ff].pb(doge0.ss);
doges[doge1.ff].pb(doge1.ss);
for(int i = 2; i < m; i++){
int bi, pi; cin >> bi >> pi;
doges[bi].pb(pi);
}
for(int i = 0; i < n; i++){
sort(all(doges[i]));
doges[i].erase(unique(all(doges[i])), doges[i].end());
// cout << doges[i].size() << endl;
}
for(int i = 0; i < n; i++) memset(dist[i], INF, sizeof dist[i]);
pq.push({0,doge0.ff, 0});
dist[doge0.ff][0] = 0;
while(pq.size()){
auto[d,u,extra] = pq.top(); pq.pop();
if(dist[u][extra]<d) continue;
// cout << u << ": " << dist[u] << endl;
if(u==doge1.ff) continue;
if(extra==0) for(int x : doges[u]){
if(x >= (int)sqrt(n)){
for(int i = u+x; i < n; i+=x){
int wv = (i-u)/x;
if(dist[i][0] > d+wv)
dist[i][0] = d+wv, pq.push({dist[i][0], i, 0});
}
for(int i = u-x; i >= 0; i-=x){
int wv = (u-i)/x;
if(dist[i][0] > d+wv)
dist[i][0] = d+wv, pq.push({dist[i][0], i, 0});
}
}
else{
if(u+x < n && dist[u+x][0] > d+1){
dist[u+x][0] = d+1, pq.push({dist[u+x][0], u+x, 0});
}
if(u+x < n && dist[u+x][x] > d+1){
dist[u+x][x] = d+1, pq.push({dist[u+x][x], u+x, x});
}
if(u-x >= 0 && dist[u-x][0] > d+1){
dist[u-x][0] = d+1, pq.push({dist[u-x][0], u-x, 0});
}
if(u-x >= 0 && dist[u-x][x] > d+1){
dist[u-x][x] = d+1, pq.push({dist[u-x][x], u-x, x});
}
}
}
if(extra){
if(u+extra < n && dist[u+extra][extra] > d+1){
dist[u+extra][extra] = d+1, pq.push({d+1, u+extra, extra});
}
if(u+extra < n && dist[u+extra][0] > d+1){
dist[u+extra][0] = d+1, pq.push({d+1, u+extra, 0});
}
if(u-extra >= 0 && dist[u-extra][extra] > d+1){
dist[u-extra][extra] = d+1, pq.push({d+1, u-extra, extra});
}
if(u-extra >= 0 && dist[u-extra][0] > d+1){
dist[u-extra][0] = d+1, pq.push({d+1, u-extra, 0});
}
}
}
if(dist[doge1.ff][0] == INF) cout << -1;
else cout << dist[doge1.ff][0];
cout << '\n';
}
# | 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... |