結果

問題 No.2328 Build Walls
ユーザー umezoumezo
提出日時 2023-05-28 15:29:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,976 bytes
コンパイル時間 2,633 ms
コンパイル使用メモリ 214,480 KB
実行使用メモリ 19,460 KB
最終ジャッジ日時 2023-08-27 12:17:40
合計ジャッジ時間 8,223 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 117 ms
19,460 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

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);
  vector<vector<int>> A(h-2,vector<int>(w));
  rep(i,h-2) rep(j,w) cin>>A[i][j];
  
  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]));
    }
  }
  
  int mi=INF;
  rep(i,h-2){
    if(A[i][0]==-1) continue;
    vector<int> dis=dijkstra(f(i,0));
    rep(j,h-2) mi=min(mi,dis[f(j,w-1)]+A[i][0]);
  }
  if(mi==INF) cout<<-1<<endl;
  else cout<<mi<<endl;
  
  return 0;
}
0