結果

問題 No.3326 岩井星人の帰星
コンテスト
ユーザー mtmr_s1
提出日時 2025-11-01 17:11:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 283 ms / 2,000 ms
コード長 1,997 bytes
コンパイル時間 7,490 ms
コンパイル使用メモリ 259,556 KB
実行使用メモリ 18,088 KB
最終ジャッジ日時 2025-11-01 17:12:10
合計ジャッジ時間 14,288 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>

using namespace std;
using namespace atcoder;
typedef long long ll;
typedef modint998244353 mint;
//typedef modint1000000007 mint;

void chmin(ll& a, ll b) { a = min(a, b); }
void chmax(ll& a, ll b) { a = max(a, b); }

int N, M;
int main() {
    cin >> N >> M;
    vector<vector<int>> G(N);
    for(int i = 0; i < M; i++){
        int u, v;
        cin >> u >> v;
        u--; v--;
        G[u].push_back(v);
        G[v].push_back(u);
    }

    int L;
    cin >> L;
    priority_queue<pair<int, int>> Q;
    vector<int> State(N, -1);
    for(int i = 0; i < L; i++){
        int J, K;
        cin >> J >> K;
        J--;
        if(State[J] < K){
            State[J] = K;
            Q.push({K, J});
        }
    }

    // マルチソースBFS
    while(!Q.empty()){
        pair<int, int> now = Q.top();
        Q.pop();
        if(State[now.second] > 0 && State[now.second] == now.first){
            for(int i = 0; i < G[now.second].size(); i++){
                int neighbor = G[now.second][i];
                if(State[neighbor] < State[now.second] - 1){
                    State[neighbor] = State[now.second] - 1;
                    Q.push({State[neighbor], neighbor});
                }
            }
        }
    }
    
    // BFS
    queue<int> Q2;
    vector<int> dist(N, INT_MAX / 2);
    Q2.push(0);
    dist[0] = 0;
    while(!Q2.empty()){
        int now = Q2.front();
        Q2.pop();
        if(State[now] == -1){
            for(int i = 0; i < G[now].size(); i++){
                int neighbor = G[now][i];
                if(State[neighbor] == -1){
                    if(dist[neighbor] == INT_MAX / 2){
                        dist[neighbor] = dist[now] + 1;
                        Q2.push(neighbor);
                    }
                }
            }
        }
    }
    if(dist[N-1] == INT_MAX / 2){
        cout << "No" << endl;
    }else{
        cout << "Yes" << endl;
        cout << dist[N-1] << endl;
    }
}
0