이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 30030;
const int base = 150;
int n, m;
int f[N][base];
int d[N][base];
vector < int > doges[N];
bool minimize(int &u, int v){
if(u > v){
u = v;
return true;
}
return false;
}
int main(){
if(fopen("1.inp", "r")){
freopen("1.inp", "r", stdin);
}
scanf("%d%d", &n, &m);
int start = -1, finish = -1;
for(int i = 1; i <= m; ++i){
int u, v;
scanf("%d%d", &u, &v);
if(start == -1){
start = u;
}
else if(finish == -1){
finish = u;
}
doges[u].push_back(v);
}
#define Data pair < int, pair < int, int > >
priority_queue < Data, vector < Data >, greater < Data > > heap;
memset(f, 60, sizeof f);
f[start][0] = 0;
heap.emplace(0, make_pair(start, 0));
while(!heap.empty()){
int u = heap.top().second.first;
int x = heap.top().second.second;
heap.pop();
if(d[u][x] == 1) continue;
d[u][x] = 1;
if(u == finish){
cout << f[u][x];
return 0;
}
if(u - x >= 0){
if(minimize(f[u - x][x], f[u][x] + 1)){
heap.emplace(f[u - x][x], make_pair(u - x, x));
}
}
if(u + x < n){
if(minimize(f[u + x][x], f[u][x] + 1)){
heap.emplace(f[u + x][x], make_pair(u + x, x));
}
}
for(int val : doges[u]){
if(val < base){
if(u - val >= 0){
if(minimize(f[u - val][val], f[u][x] + 1)){
heap.emplace(f[u - val][val], make_pair(u - val, val));
}
}
if(u + val < n){
if(minimize(f[u + val][val], f[u][x] + 1)){
heap.emplace(f[u + val][val], make_pair(u + val, val));
}
}
}
else{
for(int t = 1; t < N / base; ++t){
if(u - t * val < 0 && u + t * val >= n) break;
if(u - t * val >= 0){
if(minimize(f[u - t * val][0], f[u][x] + t)){
heap.emplace(f[u - t * val][0], make_pair(u - t * val, 0));
}
}
if(u + t * val < n){
if(minimize(f[u + t * val][0], f[u][x] + t)){
heap.emplace(f[u + t * val][0], make_pair(u + t * val, 0));
}
}
}
}
}
}
cout << -1;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:23:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("1.inp", "r", stdin);
^
skyscraper.cpp:26:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
^
skyscraper.cpp:32:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
^
# | 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... |