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 n,m;
int b[30005],p[30005];
map<pair<int,int>, bool> viz,luat;
map<pair<int,int>, int> d;
vector<int> what[30005];
int str,fin;
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];
deque<pair<int,int>> q;
q.push_back({str,0});
viz[{str,0}] = true;
while (!q.empty())
{
pair<int,int> nod = q.front();
q.pop_front();
if (luat[nod])
continue;
luat[nod] = true;
int dist = d[nod];
if (nod.second != 0)
{
pair<int,int> vec;
vec = {nod.first,0};
if (!viz[vec] or d[vec] > dist)
{
viz[vec] = true;
q.push_front(vec);
d[vec] = dist;
}
vec = {nod.first + nod.second,nod.second};
if (!viz[vec] and vec.first >= 0 and vec.first < n)
{
viz[vec] = true;
q.push_back(vec);
d[vec] = dist + 1;
}
vec = {nod.first - nod.second,nod.second};
if (!viz[vec] and vec.first >= 0 and vec.first < n)
{
viz[vec] = true;
q.push_back(vec);
d[vec] = dist + 1;
}
}
else
{
for (auto it : what[nod.first])
{
pair<int,int> vec = {nod.first,it};
if (!viz[vec] or d[vec] > dist)
{
viz[vec] = true;
q.push_front(vec);
d[vec] = dist;
}
}
}
}
if (!viz[{fin,0}])
cout << -1;
else
cout << d[{fin,0}];
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... |