#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 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]);
deque<pair<int,int>> q;
q.push_back({0,0});
viz[{0,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[{1,0}])
cout << -1;
else
cout << d[{1,0}];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1112 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Incorrect |
0 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |