This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<int,pii> pip;
#define f first
#define s second
const int maxn = 3e4+10, root = 180, inf = 0x3f3f3f3f;
int b[maxn], p[maxn], dist[maxn];
vector<int> doges[maxn];
struct HASH{
size_t operator()(const pair<int,int>&x)const{
return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
}
};
unordered_set<pii, HASH> mapa;
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
int n, m;
cin >> n >> m;
for(int i=0; i<m; i++){
cin >> b[i] >> p[i];
doges[b[i]].push_back(p[i]);
}
priority_queue<pip, vector<pip>, greater<pip>> fila;
memset(dist, inf, sizeof dist);
fila.push({0, {b[0], p[0]}});
while(!fila.empty()){
int d = fila.top().f, u = fila.top().s.f, power = fila.top().s.s;
fila.pop();
if(mapa.count({u, power})) continue;
mapa.insert({u, power});
if(u + power < n) fila.push({d + 1, {u + power, power}});
if(u - power >= 0) fila.push({d + 1, {u - power, power}});
if(dist[u] == inf){
dist[u] = d;
for(auto x : doges[u]) fila.push({d, {u, x}});
}
}
int ans = dist[b[1]];
cout << (ans == inf ? -1 : ans) << "\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... |