이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int ar = 3e4 + 5;
const int N = 105;
int n, m;
int vt[ar], p[ar];
int dp[ar][N];
vector<int> adj[ar];
struct tuan {
int cost, u, step;
};
bool operator > (tuan a, tuan b) {
return a.cost > b.cost;
}
void dijktra() {
for(int i = 0; i < m; ++i) {
adj[vt[i]].push_back(i);
}
priority_queue<tuan, vector<tuan>, greater<tuan>> q;
memset(dp, 0x3f, sizeof(dp));
dp[vt[0]][0] = 0;
q.push({0, vt[0], 0});
int ans = 1e9;
while(q.size()) {
tuan top = q.top(); q.pop();
int u = top.u, cost = top.cost, step = top.step;
if(cost > dp[u][step]) continue;
if(step == 0) {
for(int v : adj[u]) {
if(p[v] >= N) {
int bonus = 0;
for(int i = u; i < n; i += p[v]) {
if(dp[i][0] > cost + bonus) {
dp[i][0] = cost + bonus;
q.push({dp[i][0], i, 0});
}
++bonus;
}
bonus = 0;
for(int i = u; i >= 0; i -= p[v]) {
if(dp[i][0] > cost + bonus) {
dp[i][0] = cost + bonus;
q.push({dp[i][0], i, 0});
}
++bonus;
}
}
else {
if(dp[u][p[v]] > cost) {
dp[u][p[v]] = cost;
q.push({dp[u][p[v]], u, p[v]});
}
}
}
}
else {
if(u - step >= 0 && dp[u - step][step] > cost + 1) {
dp[u - step][step] = cost + 1;
q.push({dp[u - step][step], u - step, step});
}
if(u + step < n && dp[u + step][step] > cost + 1) {
dp[u + step][step] = cost + 1;
q.push({dp[u + step][step], u + step, step});
}
if(dp[u][0] > cost) {
dp[u][0] = cost;
q.push({dp[u][0], u, 0});
}
}
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i = 0; i < m; ++i) {
cin >> vt[i] >> p[i];
}
dijktra();
int ans = 1e9;
for(int i = 0; i < N; ++i) {
ans = min(ans, dp[vt[1]][i]);
}
if(ans == 1e9) ans = -1;
cout << ans;
}
컴파일 시 표준 에러 (stderr) 메시지
skyscraper.cpp: In function 'void dijktra()':
skyscraper.cpp:27:9: warning: unused variable 'ans' [-Wunused-variable]
27 | int ans = 1e9;
| ^~~
# | 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... |