#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
using namespace std;
const int maxn = 2100;
int dist[maxn][maxn];
bool visited[maxn][maxn];
int st[maxn], p[maxn], n, m;
set<int>ps[maxn];
int main() {
cin>>n>>m;
for(int i=0;i<m;i++) {
cin>>st[i]>>p[i];
ps[st[i]].insert(p[i]);
}
queue<pair<int,int> > q;
q.push(mp(st[0], p[0]));
visited[st[0]][0] = true;
while(!q.empty()) {
int currx = q.front().first;
int currc = q.front().second;
q.pop();
if(currx - currc >= 0 && !visited[currx-currc][currc]) {
visited[currx-currc][currc] = true;
dist[currx-currc][currc] = dist[currx][currc] + 1;
q.push(mp(currx-currc, currc));
}
if(currx + currc < n && !visited[currx+currc][currc]) {
visited[currx+currc][currc] = true;
dist[currx+currc][currc] = dist[currx][currc] + 1;
q.push(mp(currx+currc, currc));
}
for(int i: ps[currx]) {
if(currx - i >= 0 && !visited[currx-i][i]) {
visited[currx-i][i] = true;
dist[currx-i][i] = dist[currx][currc] + 1;
q.push(mp(currx-i, i));
}
if(currx + i < n && !visited[currx+i][i]) {
visited[currx+i][i] = true;
dist[currx+i][i] = dist[currx][currc] + 1;
q.push(mp(currx+i, i));
}
}
}
int result = INT_MAX;
for(int i=0;i<maxn;i++) {
if(visited[st[1]][i]) result = min(result, dist[st[1]][i]);
}
cout<<result<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
704 KB |
Output is correct |
2 |
Correct |
2 ms |
704 KB |
Output is correct |
3 |
Correct |
2 ms |
900 KB |
Output is correct |
4 |
Incorrect |
2 ms |
900 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
900 KB |
Output is correct |
2 |
Correct |
2 ms |
900 KB |
Output is correct |
3 |
Correct |
2 ms |
936 KB |
Output is correct |
4 |
Incorrect |
2 ms |
972 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
992 KB |
Output is correct |
2 |
Correct |
2 ms |
992 KB |
Output is correct |
3 |
Correct |
2 ms |
1000 KB |
Output is correct |
4 |
Incorrect |
2 ms |
1000 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1028 KB |
Output is correct |
2 |
Correct |
2 ms |
1028 KB |
Output is correct |
3 |
Correct |
2 ms |
1028 KB |
Output is correct |
4 |
Incorrect |
2 ms |
1028 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1044 KB |
Output is correct |
2 |
Correct |
2 ms |
1044 KB |
Output is correct |
3 |
Correct |
2 ms |
1044 KB |
Output is correct |
4 |
Incorrect |
3 ms |
1044 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |