이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
void solve(){
int n,m;
cin>>n>>m;
vector<vector<int>> adj(n+1);
vector<pair<int,int>> v(m);
int pos = 0;
for(int i=0;i<m;i++){
int a,b;cin>>a>>b;
v[i].first = a;
v[i].second = b;
if(i == 1) pos = a;
adj[a].push_back(i);
}
priority_queue<array<int,3>,vector<array<int,3>>,greater<array<int,3>>> q;
q.push({0,v[0].first,0});
vector<vector<int>> dis(n,vector<int>(m,1e9));
while(q.size()){
array<int,3> cur = q.top(); q.pop();
if(dis[cur[1]][cur[2]] < cur[0]) continue;
for(auto ch : adj[cur[1]]){
int p = v[ch].second;
if(cur[1]+p<n && dis[cur[1]+p][ch] > cur[0]+1){
q.push({cur[0]+1,cur[1]+p,ch});
dis[cur[1]+p][ch] = cur[0]+1;
}
if(cur[1]-p>=0 && dis[cur[1]-p][ch] > cur[0]+1){
q.push({cur[0]+1,cur[1]-p,ch});
dis[cur[1]-p][ch] = cur[0]+1;
}
}
int p = v[cur[2]].second;
if(cur[1]+p<n && dis[cur[1]+p][cur[2]] > cur[0]+1){
q.push({cur[0]+1,cur[1]+p,cur[2]});
dis[cur[1]+p][cur[2]] = cur[0]+1;
}
if(cur[1]-p>=0 && dis[cur[1]-p][cur[2]] > cur[0]+1){
q.push({cur[0]+1,cur[1]-p,cur[2]});
dis[cur[1]-p][cur[2]] = cur[0]+1;
}
}
int ans = dis[v[1].first][0];
for(int i=2;i<m;i++) ans = min(ans,dis[v[1].first][i]);
cout<<(ans>=1e9?-1:ans)<<endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
}
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'void solve()':
skyscraper.cpp:11:9: warning: variable 'pos' set but not used [-Wunused-but-set-variable]
11 | int pos = 0;
| ^~~
# | 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... |