이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
# define int long long
# define fir first
# define sec second
# define pb push_back
# define endl "\n"
const int cnst = 2e5+5;
bool mutipletestcase = 0;
//bool debug = false;
void solve() {
int n, m; cin >> n >> m;
int x[n+5];
int y[n+5];
vector<int> vec[n+5];
for(int i = 1; i<=m; i++) {
cin >> x[i] >> y[i];
vec[x[i]].pb(i);
}
queue<pair<int, int>> q;
for(auto v: vec[0]) q.push({0, y[v]});
int dis[n+5][3005];
memset(dis, 0, sizeof(dis));
while(!q.empty()) {
auto[a, step] = q.front(); q.pop();
// cerr << a << " " << step << " " << dis[a][step] << endl;
if(a == x[2]) {
cout << dis[a][step] << endl;
return;
}
for(auto v: vec[a]) {
int b = a+y[v];
int c = a-y[v];
// cerr << "X: " << v << " " << b << " " << c << endl;
if(0 <= b && b < n && !dis[b][y[v]]) {
q.push({b, y[v]});
dis[b][y[v]] = dis[a][step] + 1;
}
if(0 <= c && c < n && !dis[c][y[v]]) {
q.push({c, y[v]});
dis[c][y[v]] = dis[a][step]+1;
}
}
int b = a+step;
int c = a-step;
if(0 <= b && b < n && !dis[b][step]) {
q.push({b, step});
dis[b][step] = dis[a][step] + 1;
}
if(0 <= c && c < n && !dis[c][step]) {
q.push({c, step});
dis[c][step] = dis[a][step]+1;
}
}
cout << -1 << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
int t = 1;
if(mutipletestcase) cin >> t;
while(t--) solve();
}
# | 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... |