이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define all(aa) aa.begin(), aa.end()
#define pb push_back
#define endl ("\n")
typedef long long ll;
using namespace std;
const ll INF (1000000000000000);
const int maxn = 2005, maxm = 2005;
int n, m, target, pos, sped;
bool vis[maxn][maxn];
vector<int> v[maxn];
queue<array<int, 3>> q;
void pushq(int u, int s, int dst){
if(u - s >= 0) q.push({u - s, s, dst});
if(u + s < n) q.push({u+s, s, dst});
}
int main(){
cin >> n >> m;
for(int i = 0; i < m; i++){
int b, p;
cin >> b >> p;
if(i == 0) sped = p, pos = b;
if(i != 1) v[b].pb(p);
else target = b;
}
for(int i = 0; i < n; i++){
sort(all(v[i]));
v[i].resize(unique(all(v[i])) - v[i].begin());
}
q.push({pos, sped, 0});
while(!q.empty()){
auto [u, s, dst] = q.front(); q.pop();
if(target == u) cout << dst, exit(0);
if(vis[u][s])
continue;
vis[u][s] = 1;
for(int sp : v[u])
pushq(u, sp, dst+1);
pushq(u, s, dst+1);
}
cout << -1;
}
# | 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... |