結果

問題 No.3326 岩井星人の帰星
コンテスト
ユーザー GOTKAKO
提出日時 2025-11-03 04:58:52
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 129 ms / 2,000 ms
コード長 1,217 bytes
コンパイル時間 2,166 ms
コンパイル使用メモリ 209,792 KB
実行使用メモリ 17,852 KB
最終ジャッジ日時 2025-11-03 04:59:01
合計ジャッジ時間 7,825 ms
ジャッジサーバーID
(参考情報)
judge1 / judge7
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N,M; cin >> N >> M;
    vector<vector<int>> Graph(N);
    for(int i=0; i<M; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        Graph.at(u).push_back(v);
        Graph.at(v).push_back(u);
    }

    int L; cin >> L;
    vector<int> NG(N,-1);
    while(L--){
        int j,k; cin >> j >> k;
        NG.at(j-1) = max(NG.at(j-1),k);
    }
    priority_queue<pair<int,int>> Q;
    for(int i=0; i<N; i++) if(NG.at(i) >= 0) Q.push({NG.at(i),i}); 
    while(Q.size()){
        auto [d,pos] = Q.top(); Q.pop();
        if(NG.at(pos) != d) continue;
        if(d == 0) continue;
        for(auto to : Graph.at(pos)) if(NG.at(to) < d-1) NG.at(to) = d-1,Q.push({d-1,to});
    }
    if(NG.at(0) >= 0){cout << "No\n"; return 0;}
    vector<int> dist(N,-1);
    dist.at(0) = 0;
    queue<int> q; q.push(0);
    while(q.size()){
        int pos = q.front(); q.pop();
        for(auto to : Graph.at(pos)) if(NG.at(to) == -1 && dist.at(to) == -1) dist.at(to) = dist.at(pos)+1,q.push(to);
    }

    if(dist.at(N-1) == -1) cout << "No\n";
    else cout << "Yes\n" << dist.at(N-1) << endl;
}
0