이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const int maxn = 3e4+5;
const int inf = 1e9;
map<int, int> mp;
queue<int> q;
vector<pair<int, int>> vec[maxn];
int passed[maxn];
int n, m;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//ifstream cin("input.txt");
cin >> n >> m;
for(int i = 0; i < m; i++){
int a, b;
cin >> a >> b;
vec[a].push_back({b, i});
if(!i){
mp[a*maxn+b]=0;
q.push(a*maxn+b);
}
}
int ans = -1;
while(!q.empty()){
auto x = q.front();
q.pop();
int a = x/maxn, b = x%maxn, val = mp[x], id;
if(!passed[a]){
passed[a]=1;
for(auto v: vec[a]){
id = a*maxn+v.f;
if(v.s == 1)ans = val;
if(mp.find(id) == mp.end()){
mp[id]=val;
q.push(id);
}
}
}
if(a-b >= 0){
id = (a-b)*maxn + b;
if(mp.find(id) == mp.end()){
mp[id]=val+1;
q.push(id);
}
}
if(a+b < n){
id = (a+b)*maxn + b;
if(mp.find(id) == mp.end()){
mp[id]=val+1;
q.push(id);
}
}
}
cout << ans;
}
# | 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... |