結果

問題 No.2673 A present from B
ユーザー umezo
提出日時 2024-03-16 08:14:27
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 1,513 bytes
コンパイル時間 3,192 ms
コンパイル使用メモリ 261,196 KB
実行使用メモリ 30,656 KB
最終ジャッジ日時 2024-09-30 03:53:16
合計ジャッジ時間 4,586 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

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 ll INF=1LL<<60;

struct Edge{
  int to;
  ll w;
  Edge(int to,ll w) : to(to),w(w) {}
};

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

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

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

  int n,m;
  cin>>n>>m;
  vector<int> A(m);
  rep(i,m){
    cin>>A[i];
    A[i]--;
  }
  
  Graph G(n*(m+1));
  rep(j,n){
    if(j<n-1) G[j].push_back(Edge(j+1,1));
    if(j>0) G[j].push_back(Edge(j-1,1));
  }
  rep(i,m){
    rep(j,n){
      if(j<n-1) G[(i+1)*n+j].push_back(Edge(((i+1)*n+j+1),1));
      if(j>0) G[(i+1)*n+j].push_back(Edge(((i+1)*n+j-1),1));
      if(j==A[i]) G[(i+1)*n+j].push_back(Edge((i*n+j+1),0));
      else if(j-1==A[i]) G[(i+1)*n+j].push_back(Edge((i*n+j-1),0));
      else G[(i+1)*n+j].push_back(Edge((i*n+j),0));
    }
  }
  
  vector<ll> dist(n*(m+1),INF);
  int s=n*m;
  dist[s]=0;
  
  priority_queue<pli,vector<pli>,greater<pli>> que;
  que.push({dist[s],s});
  
  while(!que.empty()){
    int v=que.top().second;
    ll 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});
      }
    }
  }
  for(int i=1;i<n;i++){
    if(i>1) cout<<" ";
    cout<<dist[i];
  }
  cout<<endl;
  
  return 0;
}
0