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;
int hsh(pair<int,int> pr)
{
return pr.first * 31000 + pr.second;
}
int n,m;
int b[30005],p[30005];
unordered_map<int, bool> luat;
bitset<1000000000> viz;
unordered_map<int, int> d;
vector<int> what[30005];
int str,fin;
pair<int,int> q[2000005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 1; i <= m; i++)
cin >> b[i] >> p[i],what[b[i]].push_back(p[i]);
str = b[1],fin = b[2];
int bk = 1e6,fr = 1e6;
q[bk] = {str,0};
viz[hsh({str,0})] = 1;
int cate = 0,lul = 1e6;
while (bk <= fr)
{
pair<int,int> nod = q[fr];
//cout << nod.first << ' ' << nod.second << endl;
int dehnod = hsh(nod);
fr--;
if (luat[dehnod])
continue;
if (nod.first == fin and nod.second == 0)
break;
cate++;
//if (cate > lul)
// assert(false);
luat[dehnod] = true;
int dist = d[dehnod];
if (nod.second != 0)
{
pair<int,int> vec;
vec = {nod.first,0};
int dehvec = hsh(vec);
if (!viz[dehvec] or d[dehvec] > dist)
{
viz[dehvec] = 1;
q[++fr] = (vec);
d[dehvec] = dist;
}
vec = {nod.first + nod.second,nod.second};
dehvec = hsh(vec);
if (vec.first >= 0 and vec.first < n and !viz[dehvec])
{
viz[dehvec] = 1;
q[--bk] = vec;
d[dehvec] = dist + 1;
}
vec = {nod.first - nod.second,nod.second};
dehvec = hsh(vec);
if (vec.first >= 0 and vec.first < n and !viz[dehvec])
{
viz[dehvec] = 1;
q[--bk] = vec;
d[dehvec] = dist + 1;
}
}
else
{
for (auto it : what[nod.first])
{
pair<int,int> vec = {nod.first,it};
int dehvec = hsh(vec);
if (!viz[dehvec] or d[dehvec] > dist)
{
viz[dehvec] = 1;
q[++fr] = vec;
d[dehvec] = dist;
}
}
}
}
if (!viz[hsh({fin,0})])
cout << -1;
else
cout << d[hsh({fin,0})];
return 0;
}
Compilation message (stderr)
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:31:18: warning: unused variable 'lul' [-Wunused-variable]
31 | int cate = 0,lul = 1e6;
| ^~~
# | 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... |