結果
| 問題 | 
                            No.2328 Build Walls
                             | 
                    
| コンテスト | |
| ユーザー | 
                             MM
                         | 
                    
| 提出日時 | 2023-06-02 00:56:33 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                TLE
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,355 bytes | 
| コンパイル時間 | 2,143 ms | 
| コンパイル使用メモリ | 179,220 KB | 
| 実行使用メモリ | 228,640 KB | 
| 最終ジャッジ日時 | 2024-12-28 15:22:49 | 
| 合計ジャッジ時間 | 55,071 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 22 TLE * 12 | 
ソースコード
#include<bits/stdc++.h>
#define chmin(x,y) (x) = min((x),(y))
using namespace std;
using ll = long long;
bool debug = 0;
const vector<int> dx = {1,1,0,-1,-1,-1,0,1}, dy = {0,1,1,1,0,-1,-1,-1};
int main(){
  // input
  int H,W; cin >> H >> W;
  vector<vector<ll>> A(H,vector<ll>(W,-1));
  for (int i = 1; i + 1 < H; i++)
    for (int j = 0; j < W; j++)
      cin >> A[i][j];
  
  // solve: dijkstra
  vector<vector<ll>> dis(H,vector<ll>(W,9e18));
  priority_queue<vector<ll>> pq; // {cost,cur_x,cur_y}
  for (int i = 1; i + 1 < H; i++){
    if(A[i][0] != -1)
      pq.push({-A[i][0],i,0});
  }
  
  while(!pq.empty()){
    ll cost = -pq.top()[0], cur_x = pq.top()[1], cur_y = pq.top()[2];
    pq.pop();
    chmin(dis[cur_x][cur_y],cost);
    for(int j = 0; j < 8; j++){
      ll nxt_x = cur_x + dx[j], nxt_y = cur_y + dy[j];
      if(nxt_x < 0 || nxt_x >= H || nxt_y < 0 || nxt_y >= W) continue;
      if(A[nxt_x][nxt_y] == -1) continue;
      
      if(dis[nxt_x][nxt_y] > cost + A[nxt_x][nxt_y]){
        pq.push({-(cost+A[nxt_x][nxt_y]),nxt_x,nxt_y});
        if(debug) cout << nxt_x << " " << nxt_y << " " << cost + A[nxt_x][nxt_y] << endl;
      }
    }
  }
  
  // output
  ll ans = 9e18;
  for (int i = 0; i < H; i++){
    if(debug) cout << i << ":" << dis[i][W-1] << endl;
    chmin(ans,dis[i][W-1]);
  }
  cout << (ans < 2e9? ans : -1) << endl;
}
            
            
            
        
            
MM