結果

問題 No.3263 違法な散歩道
ユーザー Drosera
提出日時 2025-09-07 04:40:16
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,675 bytes
コンパイル時間 3,260 ms
コンパイル使用メモリ 289,588 KB
実行使用メモリ 15,944 KB
最終ジャッジ日時 2025-09-07 04:40:25
合計ジャッジ時間 9,061 ms
ジャッジサーバーID
(参考情報)
judge4 / judge
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 5 TLE * 1 -- * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(int)n; i++)
#define repi(i,m,n) for(int i=(int)m; i<=(int)n; i++)
#define rrep(i,n) for(int i=(int)n-1; i>=0; i--)
#define rrepi(i,m,n) for(int i=(int)n; i>=(int)m; i--)
#define all(x) x.begin(),x.end()
template <typename T> inline bool chmin(T& a, const T& b) { return a > b ? a = b, true : false; };
template <typename T> inline bool chmax(T& a, const T& b) { return a < b ? a = b, true : false; };
template <typename T> void uniq(std::vector<T> &v){
    std::sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
}
using ll = long long;

int main(){
    int n,m; cin >> n >> m;
    vector<vector<int>> g(n);
    rep(i,m){
        int u,v; cin >> u >> v;
        u--; v--;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }
    
    int k; cin >> k;
    vector<bool> y(n, false);
    rep(i,k){
        int a; cin >> a;
        a--;
        y[a] = true;
    }
    
    const int INF = 1e9;
    vector<vector<int>> memo(5, vector<int> (n, INF));
    memo[0][0] = 0;
    priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
    pq.emplace(0,0);
    while(!pq.empty()){
        auto [dist, id] = pq.top(); pq.pop();
        int p = id%n;
        int m = id/n;
        if(memo[m][p] < dist) continue;
        memo[m][p] = dist;
        for(auto to : g[p]){
            int c = (y[to] ? m + 1 : 0);
            if(c == 5) continue;
            if(memo[c][to] == INF) pq.emplace(dist + 1, c*n + to);                                    
        }
    }
    
    cout << (memo[0][n-1] == INF ? -1 : memo[0][n-1]) << endl;
    
    return 0;
}
0