#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll INF=1LL<<60; typedef pair P; typedef pair PP; const ll MOD=1e9+7; //const int dx[]={-1,0,1,0}; //const int dy[]={0,-1,0,1}; const int dx[]={-1,0,1,1,1,0,-1,-1}; const int dy[]={-1,-1,-1,0,1,1,1,0}; struct edge{ int to; ll cost; edge(int to_,ll cost_):to(to_),cost(cost_){}; }; int main(){ int H,W; cin>>H>>W; vector> A(H-1-2+1,vector(W,-1)); for(int i=0;i>A[i][j]; } } vector> G(H*W); for(int i=0;i=0){ G[i*W+(W-1)].emplace_back(end,A[i][W-1]); } } //dijkstra vector dp(H*W,INF); priority_queue,greater

> pq; pq.emplace(0,start); while(!pq.empty()){ auto [cost,now]=pq.top(); pq.pop(); if(dp[now]<=cost) continue; //dp[now]>cost dp[now]=cost; for(edge e:G[now]){ if(dp[e.to]>(dp[now]+e.cost)){ pq.emplace(dp[now]+e.cost,e.to); } } } cout<<(dp[end]==INF?-1:dp[end])<