Submission #768351

#TimeUsernameProblemLanguageResultExecution timeMemory
768351hgmhc날다람쥐 (JOI14_ho_t4)C++17
25 / 100
7 ms1384 KiB
#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]]);
}

Compilation message (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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...