이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define task "test1"
#define minpos(x, y) (a[x] <= a[y] ? (x) : (y))
#define pll pair<ll, ll>
#define pii pair<int, int>
using namespace std;
int const nmax = 5e4+3;
int const mod = 1e9+7;
int const LG = 20;
int const bl=100;
void open_file()
{
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
}
void fast()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
struct ok{
int dis,i,p;
};
bool operator > (ok a,ok b){
return a.dis>b.dis;
}
int n,m;
int b[nmax],p[nmax];
int dp[nmax][bl+1];
vector<int> doge[nmax];
void input()
{
cin >> n >> m;
for(int i=0;i<m;i++){
cin >> b[i] >> p[i];
doge[b[i]].push_back(i);
}
}
void NGU()
{
int ans=1e9;
priority_queue<ok,vector<ok>,greater<ok>> q;
q.push({0,b[0],0});
memset(dp,0x3f,sizeof(dp));
dp[b[0]][0]=0;
while(!q.empty()){
ok top=q.top();
q.pop();
int dis=top.dis;
int pk=top.p;
int i=top.i;
if(i==b[1]){
ans=min(ans,dis);
continue;
}
if(dis!=dp[i][pk]) continue;
if(pk==0){
for(auto x : doge[i]){
if(p[x]<=bl){
if(dp[i][p[x]]>dis){
dp[i][p[x]]=dis;
q.push({dp[i][p[x]],i,p[x]});
}
}
else{
int bc=p[x];
int cnt=0;
for(int j=x;j<=n;j+=bc){
if(dp[j][0]>dis+cnt){
dp[j][0]=dis+cnt;
q.push({dp[j][0],j,0});
}
cnt++;
}
cnt=0;
for(int j=x;j>=0;j-=bc){
if(dp[j][0]>dis+cnt){
dp[j][0]=dis+cnt;
q.push({dp[j][0],j,0});
}
cnt++;
}
}
}
}
else{
if(i+pk<n && dp[i+pk][pk]>dis+1){
dp[i+pk][pk]=dis+1;
q.push({dp[i+pk][pk],i+pk,pk});
}
if(i-pk>0 && dp[i-pk][pk]>dis+1){
dp[i-pk][pk]=dis+1;
q.push({dp[i-pk][pk],i-pk,pk});
}
if(dp[i][0]>dis){
dp[i][0]=dis;
q.push({dis,i,0});
}
}
}
cout << (ans!=1e9 ? ans:-1);
}
int main()
{
open_file();
fast();
input();
NGU();
}
/*
1 2 5 7 7
*/
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'void open_file()':
skyscraper.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:22:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | 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... |