結果

問題 No.3263 違法な散歩道
ユーザー umezo
提出日時 2025-10-09 00:40:34
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 246 ms / 2,000 ms
コード長 1,468 bytes
コンパイル時間 3,126 ms
コンパイル使用メモリ 289,000 KB
実行使用メモリ 44,184 KB
最終ジャッジ日時 2025-10-09 00:40:44
合計ジャッジ時間 9,615 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

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;
template<class T>using V=vector<T>;
template<class T>using VV=V<V<T>>;

const ll INF=1e18;
struct Edge{
  int to;
  ll w;
  Edge(int to,ll w):to(to),w(w){}
};
using Graph=VV<Edge>;
using pli=pair<ll,int>;
template<class T> bool chmin(T& a,T b){
  return ((a>b)?(a=b,true):(false));
}

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

  int n,m;
  cin>>n>>m;
  V<int> u(m),v(m);
  rep(i,m){
    cin>>u[i]>>v[i];
    u[i]--,v[i]--;
  }
  int k;
  cin>>k;
  V<int> A(n);
  rep(i,k){
    int a;
    cin>>a;
    a--;
    A[a]=1;
  }
  Graph G(5*n);
  rep(i,m){
    int a=u[i],b=v[i];
    rep(j,5){
      if(A[b]==0) G[a+j*n].push_back(Edge(b,1));
      else if(j<4) G[a+j*n].push_back(Edge(b+(j+1)*n,1));
      if(A[a]==0) G[b+j*n].push_back(Edge(a,1));
      else if(j<4) G[b+j*n].push_back(Edge(a+(j+1)*n,1));
    }
  }

  V<ll> dist(5*n,INF);
  int s=0;
  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});
      }
    }
  }
  ll ans=INF;
  rep(i,5) ans=min(ans,dist[n-1+i*n]);
  if(ans==INF) cout<<-1<<endl;
  else cout<<ans<<endl;

  return 0;
}
0