結果

問題 No.1565 Union
ユーザー otoshigootoshigo
提出日時 2023-02-23 12:57:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 1,015 bytes
コンパイル時間 902 ms
コンパイル使用メモリ 84,764 KB
実行使用メモリ 14,928 KB
最終ジャッジ日時 2023-09-30 15:28:11
合計ジャッジ時間 6,151 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 25 ms
5,240 KB
testcase_11 AC 66 ms
11,352 KB
testcase_12 AC 54 ms
6,724 KB
testcase_13 AC 19 ms
5,420 KB
testcase_14 AC 81 ms
9,836 KB
testcase_15 AC 128 ms
14,472 KB
testcase_16 AC 109 ms
14,056 KB
testcase_17 AC 128 ms
14,472 KB
testcase_18 AC 132 ms
14,260 KB
testcase_19 AC 130 ms
14,324 KB
testcase_20 AC 61 ms
14,856 KB
testcase_21 AC 60 ms
14,804 KB
testcase_22 AC 61 ms
14,832 KB
testcase_23 AC 61 ms
14,912 KB
testcase_24 AC 60 ms
14,772 KB
testcase_25 AC 63 ms
14,804 KB
testcase_26 AC 61 ms
14,720 KB
testcase_27 AC 63 ms
14,928 KB
testcase_28 AC 63 ms
14,860 KB
testcase_29 AC 62 ms
14,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
template<class T> bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;}
template<class T> bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,M;
    cin>>N>>M;
    vector<vector<int>>g(N);
    rep(i,M){
        int a,b;
        cin>>a>>b;
        a--;
        b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    vector<int>d(N,1<<30);
    d[0]=0;
    queue<pair<int,int>>q;
    q.push(make_pair(0,0));
    while(q.size()){
        auto[c,x]=q.front();
        q.pop();
        for(auto i:g[x]){
            if(chmin(d[i],c+1))q.push(make_pair(d[i],i));
        }
    }
    if(d[N-1]==1<<30)cout<<-1<<"\n";
    else cout<<d[N-1]<<"\n";
}
0