結果

問題 No.3263 違法な散歩道
ユーザー ATM
提出日時 2025-09-06 13:42:18
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 8,235 bytes
コンパイル時間 4,455 ms
コンパイル使用メモリ 265,576 KB
実行使用メモリ 16,632 KB
最終ジャッジ日時 2025-09-06 13:42:28
合計ジャッジ時間 7,327 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef INCLUDED_MAIN
#define INCLUDED_MAIN

#include __FILE__ // このファイル自体をインクルード

void solve()
{
    ll N, M;
    cin >> N >> M;
    using Graph = vector<vector<ll>>;
    Graph G(N);
    REP(i, M)
    {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        G[u].pb(v);
        G[v].pb(u);
    }
    ll K;
    cin >> K;
    vector<bool> A(N);
    REP(i, K)
    {
        ll x;
        cin >> x;
        x--;
        A[x] = true;
    }

    VVI seen(N, VI(5, INF));
    queue<pair<int, int>> que;
    que.push({0, 0}); // sから探索する
    seen[0][0] = 0;
    while (que.size() != 0)
    {                                     // 幅優先探索
        auto [state, step] = que.front(); // 現在の状態
        que.pop();
        for (auto next : G[state])
        {
            int nstep = step;
            if (A[next])
            {
                nstep++;
            }
            else
            {
                nstep = 0;
            }
            if (nstep == 5)
            {
                continue;
            }
            if (seen[next][nstep] == INF)
            { // 未探索の時のみ行う
                seen[next][nstep] = seen[state][step] + 1;
                que.emplace(next, nstep); // 次の状態をqueueへ格納
            }
        }
    }
    ll ans = INF;
    REP(i, 5)
    chmin(ans, seen[N - 1][i]);
    if (ans == INF)
    {
        cout << -1 << endl;
    }
    else
    {
        cout << ans << endl;
    }
}

signed main()
{

    solve();

    return 0;
}

#else // INCLUDED_MAIN

using namespace std;
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x, y) CPP_CAT_I(x, y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x, y) x##y

#define ASSERT(expr...) assert((expr))

using i8 = int8_t;
using u8 = uint8_t;
using i16 = int16_t;
using u16 = uint16_t;
using i32 = int32_t;
using u32 = uint32_t;
using i64 = int64_t;
using u64 = uint64_t;

using f32 = float;
using f64 = double;
// }}}

constexpr i64 INF = 1'010'000'000'000'000'017LL;
constexpr int inf = 1073741823;

constexpr i64 MOD = 998244353LL;

constexpr f64 EPS = 1e-12;

constexpr f64 PI = 3.14159265358979323846;

#define M5 100007
#define M9 1000000000

#define F first
#define S second

// util {{{
#define FOR(i, start, end) for (i64 i = (start), CPP_CAT(i, xxxx_end) = (end); i < CPP_CAT(i, xxxx_end); ++i)
#define RFOR(i, start, end) for (ll i = start - 1; i >= end; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ll long long int
#define VI vector<ll>
#define VVI vector<VI>
#define VC vector<char>
#define VVC vector<VC>
#define VS vector<string>
#define VVS vector<VS>
#define VB vector<bool>
#define VVB vector<VB>
#define VM vector<mint>
#define VVM vector<VM>
#define vec vector
#define pb push_back

#define ISD true
#define debug(x) \
    if (ISD)     \
    cout << #x << ": " << x << endl
#define DEBUG(x) cout << #x << ": " << x << endl

template <typename C>
i64 SIZE(const C &c)
{
    return static_cast<i64>(c.size());
}

template <typename T, size_t N>
i64 SIZE(const T (&)[N]) { return static_cast<i64>(N); }

template <typename T, typename U, typename Comp = less<>>
bool chmax(T &xmax, const U &x, Comp comp = {})
{
    if (comp(xmax, x))
    {
        xmax = x;
        return true;
    }
    return false;
}

template <typename T, typename U, typename Comp = less<>>
bool chmin(T &xmin, const U &x, Comp comp = {})
{
    if (comp(x, xmin))
    {
        xmin = x;
        return true;
    }
    return false;
}
// }}}

// init {{{
struct ProconInit
{
    static constexpr int IOS_PREC = 15;
    static constexpr bool AUTOFLUSH = false;

    ProconInit()
    {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(IOS_PREC);
        if (AUTOFLUSH)
            cout << unitbuf;
    }
} PROCON_INIT;
// }}}

//--------------------------------------------------------------------

bool isKaibun(string st)
{
    int n = st.length();
    REP(i, n / 2)
    {
        if (st[i] != st[n - i - 1])
        {
            return false;
        }
    }
    return true;
}

int toInt(char a)
{
    return a - '0';
}

ll gcd(ll a, ll b)
{
    if (a % b == 0)
    {
        return (b);
    }
    else
    {
        return (gcd(b, a % b));
    }
}

vector<long long> enum_divisors(long long N)
{
    vector<long long> res;
    for (long long i = 1; i * i <= N; ++i)
    {
        if (N % i == 0)
        {
            res.push_back(i);
            // 重複しないならば i の相方である N/i も push
            if (N / i != i)
                res.push_back(N / i);
        }
    }
    // 小さい順に並び替える
    sort(res.begin(), res.end());
    return res;
}

vector<pair<long long, long long>> prime_factorize(long long N)
{
    vector<pair<long long, long long>> res;
    for (long long a = 2; a * a <= N; ++a)
    {
        if (N % a != 0)
            continue;
        long long ex = 0; // 指数

        // 割れる限り割り続ける
        while (N % a == 0)
        {
            ++ex;
            N /= a;
        }

        // その結果を push
        res.push_back({a, ex});
    }

    // 最後に残った数について
    if (N != 1)
        res.push_back({N, 1});
    return res;
}

long long GCD(long long a, long long b)
{
    if (b == 0)
        return a;
    else
        return GCD(b, a % b);
}

ll lcm(ll a, ll b)
{
    return a * b / GCD(a, b);
}

long long safe_pow(long long a, long long b)
{
    long long res = 1;
    for (long long i = 0; i < b; i++)
    {
        double dres = res;
        dres *= a;
        if (dres > 2e18)
        {
            return 2e18;
        }
        res *= a;
    }
    return res;
}
long long sqrt_floor(long long x)
{
    long long l = 0, r = 2e9;
    while (l <= r)
    {
        long long t = (l + r) / 2;
        if (t * t > x)
        {
            r = t - 1;
        }
        else
        {
            l = t + 1;
        }
    }
    return r;
}

ll popcount(ll bit)
{
    ll c = 0;
    while (bit > 0)
    {
        c += bit % 2;
        bit /= 2;
    }
    return c;
}

bool on(ll N, ll K)
{
    return N & (1LL << K);
}

template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, std::pair<T1, T2> p)
{
    os << "{" << p.first << "," << p.second << "}";
    return os;
}

bool outof(ll y, ll x, ll h, ll w)
{
    return y < 0 || x < 0 || y >= h || x >= w;
};

// 配列の要素を空白区切りで出力 第二引数をtrueにすると改行区切り
template <typename T>
inline void pv(const vector<T> &v, bool split_line = false)
{
    if (v.empty())
    {
        cout << "This vector is empty." << endl;
        return;
    }
    const bool isValue = is_integral<T>::value;
    for (int i = 0; i < (int)v.size(); i++)
    {
        if (isValue)
        {
            if ((v[i] == inf) || (v[i] == INF))
                cout << 'x' << " \n"[split_line || i + 1 == (int)v.size()];
            else
                cout << v[i] << " \n"[split_line || i + 1 == (int)v.size()];
        }
        else
            cout << v[i] << " \n"[split_line || i + 1 == (int)v.size()];
    }
}

template <typename T1, typename T2>
inline void pv(const vector<pair<T1, T2>> &v, bool split_line = false)
{
    if (v.empty())
    {
        cout << "This vector is empty." << endl;
        return;
    }
    for (int i = 0; i < (int)v.size(); i++)
    {
        cout << '{';
        auto a = v[i].F;
        auto b = v[i].S;
        pair<bool, bool> isValue = {is_integral<T1>::value, is_integral<T2>::value};
        if (isValue.first)
        {
            if (a == inf || a == INF)
                cout << "x,";
            else
                cout << a << ",";
        }
        else
            cout << a << ",";
        if (isValue.second)
        {
            if (b == inf || b == INF)
                cout << "x,";
            else
                cout << b;
        }
        else
            cout << b;
        cout << "}" << " \n"[split_line || i + 1 == (int)v.size()];
    }
}

#endif // INCLUDED_MAIN
0