結果

問題 No.2328 Build Walls
ユーザー umezoumezo
提出日時 2023-05-28 16:10:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 286 ms / 3,000 ms
コード長 2,067 bytes
コンパイル時間 2,441 ms
コンパイル使用メモリ 216,368 KB
実行使用メモリ 72,704 KB
最終ジャッジ日時 2024-06-08 09:20:25
合計ジャッジ時間 6,217 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 54 ms
19,456 KB
testcase_14 AC 66 ms
19,968 KB
testcase_15 AC 65 ms
15,104 KB
testcase_16 AC 10 ms
5,376 KB
testcase_17 AC 51 ms
15,104 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 7 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 28 ms
13,568 KB
testcase_22 AC 102 ms
28,544 KB
testcase_23 AC 229 ms
48,896 KB
testcase_24 AC 213 ms
45,312 KB
testcase_25 AC 232 ms
48,512 KB
testcase_26 AC 169 ms
38,272 KB
testcase_27 AC 198 ms
43,264 KB
testcase_28 AC 52 ms
23,296 KB
testcase_29 AC 226 ms
49,024 KB
testcase_30 AC 97 ms
33,408 KB
testcase_31 AC 84 ms
30,848 KB
testcase_32 AC 193 ms
42,624 KB
testcase_33 AC 286 ms
72,704 KB
testcase_34 AC 111 ms
34,560 KB
testcase_35 AC 258 ms
60,416 KB
testcase_36 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
 
#include<bits/stdc++.h>
using namespace std;

const int INF=1e9;
struct Edge{
  int to;
  int w;
  Edge(int to,ll w) : to(to),w(w) {}
};

using Graph=vector<vector<Edge>>;
using pli=pair<int,int>;

template<class T> bool chmin(T& a,T b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}

Graph G;
vector<int> dijkstra(int s){
  int n=G.size();
  vector<int> dist(n,INF);
  dist[s]=0;
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    int d=que.top().first;
    que.pop();
    
    if(d>dist[v]) continue;
    
    for(auto e:G[v]){
      if(chmin(dist[e.to],dist[v]+e.w)){
        que.push({dist[e.to],e.to});
      }
    }
  }
  return dist;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int h,w;
  cin>>h>>w;
  
  auto f=[&](int x,int y){
    return x*w+y;
  };
  
  G.resize((h-2)*w+2);
  int s=(h-2)*w,t=s+1;
  vector<vector<int>> A(h-2,vector<int>(w));
  rep(i,h-2) rep(j,w) cin>>A[i][j];
  
  rep(i,h-2){
    if(A[i][0]!=0) G[s].push_back(Edge(f(i,0),A[i][0]));
  }
  rep(i,h-2){
    if(A[i][w-1]!=0) G[f(i,w-1)].push_back(Edge(t,0));
  }                               
  rep(i,h-2) rep(j,w){
    if(A[i][j]==-1) continue;
    if(i<h-3 && A[i+1][j]!=-1){
      G[f(i,j)].push_back(Edge(f(i+1,j),A[i+1][j]));
      G[f(i+1,j)].push_back(Edge(f(i,j),A[i][j]));
    }
    if(i<h-3 && j<w-1 && A[i+1][j+1]!=-1){
      G[f(i,j)].push_back(Edge(f(i+1,j+1),A[i+1][j+1]));
      G[f(i+1,j+1)].push_back(Edge(f(i,j),A[i][j]));
    }
    if(j<w-1 && A[i][j+1]!=-1){
      G[f(i,j)].push_back(Edge(f(i,j+1),A[i][j+1]));
      G[f(i,j+1)].push_back(Edge(f(i,j),A[i][j]));
    }
    if(i && j<w-1 && A[i-1][j+1]!=-1){
      G[f(i,j)].push_back(Edge(f(i-1,j+1),A[i-1][j+1]));
      G[f(i-1,j+1)].push_back(Edge(f(i,j),A[i][j]));
    }
  }
  vector<int> dis=dijkstra(s);
  if(dis[t]==INF) cout<<-1<<endl;
  else cout<<dis[t]<<endl;
  
  return 0;
}
0