이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define rep(i,a,b) for (auto i = (a); i <= (b); ++i)
#define Mup(x,y) x = max(x,y)
#define mup(x,y) x = min(x,y)
#define all(x) begin(x),end(x)
#define per(i,a,b) for (auto i = (b); i >= (a); --i)
using namespace std;
using ll = long long;
using ii = pair<int,int>;
#define dbg(...) fprintf(stderr,__VA_ARGS__)
#define mp make_pair
const ll INF = 1e18;
const int N = 1003, H = 103;
int n, m, x, h[N];
vector<ii> adj[N];
ll dist[N][H];
int main() {
scanf("%d%d%d", &n, &m, &x);
rep(i,1,n) scanf("%d", &h[i]), assert(h[i] <= 100);
rep(i,1,m) {
int u, v, t;
scanf("%d%d%d", &u, &v, &t);
adj[u].push_back({v,t});
adj[v].push_back({u,t});
}
fill(dist[0],dist[N],INF);
priority_queue<tuple<ll,int,int>> pq;
dist[1][x] = 0, pq.push({0,1,x});
while (not empty(pq)) {
auto [d,a,x] = pq.top(); pq.pop(), d=-d;
if (dist[a][x] < d) continue;
if (0 < x and dist[a][x-1] > dist[a][x]+1) {
dist[a][x-1] = dist[a][x]+1;
pq.push({-dist[a][x-1],a,x-1});
}
if (x < h[a] and dist[a][x+1] > dist[a][x]+1) {
dist[a][x+1] = dist[a][x]+1;
pq.push({-dist[a][x+1],a,x+1});
}
for (auto [b,w] : adj[a]) {
if (0 <= x-w and x-w <= h[b]) {
if (dist[b][x-w] > dist[a][x]+w) {
dist[b][x-w] = dist[a][x]+w;
pq.push({-dist[b][x-w],b,x-w});
}
}
}
}
if (dist[n][h[n]] >= INF) puts("-1");
else printf("%lld", dist[n][h[n]]);
}
컴파일 시 표준 에러 (stderr) 메시지
2014_ho_t4.cpp: In function 'int main()':
2014_ho_t4.cpp:20:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d%d%d", &n, &m, &x);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
2014_ho_t4.cpp:21:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | rep(i,1,n) scanf("%d", &h[i]), assert(h[i] <= 100);
| ~~~~~^~~~~~~~~~~~~
2014_ho_t4.cpp:24:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | scanf("%d%d%d", &u, &v, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |