結果

問題 No.1565 Union
ユーザー maguromaguro
提出日時 2021-04-21 10:22:52
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 2,417 bytes
コンパイル時間 2,311 ms
コンパイル使用メモリ 209,336 KB
実行使用メモリ 21,252 KB
最終ジャッジ日時 2023-09-07 16:11:46
合計ジャッジ時間 7,104 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,392 KB
testcase_01 AC 4 ms
8,388 KB
testcase_02 AC 4 ms
8,388 KB
testcase_03 AC 4 ms
8,344 KB
testcase_04 AC 3 ms
8,448 KB
testcase_05 AC 4 ms
8,664 KB
testcase_06 AC 4 ms
8,392 KB
testcase_07 AC 4 ms
8,400 KB
testcase_08 AC 4 ms
8,448 KB
testcase_09 AC 4 ms
8,672 KB
testcase_10 AC 70 ms
14,624 KB
testcase_11 AC 127 ms
16,772 KB
testcase_12 AC 134 ms
18,648 KB
testcase_13 AC 40 ms
11,548 KB
testcase_14 AC 174 ms
18,932 KB
testcase_15 AC 267 ms
21,124 KB
testcase_16 AC 202 ms
20,848 KB
testcase_17 AC 261 ms
21,112 KB
testcase_18 AC 270 ms
21,252 KB
testcase_19 AC 264 ms
21,072 KB
testcase_20 AC 144 ms
19,788 KB
testcase_21 AC 142 ms
19,848 KB
testcase_22 AC 143 ms
19,792 KB
testcase_23 AC 143 ms
19,792 KB
testcase_24 AC 142 ms
19,804 KB
testcase_25 AC 156 ms
19,736 KB
testcase_26 AC 150 ms
19,808 KB
testcase_27 AC 155 ms
19,796 KB
testcase_28 AC 157 ms
19,844 KB
testcase_29 AC 152 ms
19,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 2000000030;
constexpr ll INF= 2000000000000000001;
constexpr ll MOD = 1000000007;
const double PI = 3.14159265358979323846;
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;

template<typename T> 
vector<T> make_vector(size_t s) {
    return vector<T>(s);
}

template<typename T,typename... Args>
auto make_vector(size_t s,Args... args) {
    return vector(s,make_vector<T>(args...));
}

template<typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}

template<typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

ll mod(ll val, ll M) {
    val = val % M;
    if(val < 0) {
        val += M;
    }
    return val;
}

template<typename T>
T modpow(T N, T P, T M){
    if(P == 0) return 1;
    if(P < 0) return 0;
    if(P % 2 == 0){
        ll t = RS(N, P/2, M);
        if(M == -1) return t * t;
        return t * t % M;
    }
    if(M == -1) return N * modpow(N,P - 1,M);
    return N * modpow(N, P-1, M) % M;
}

struct edge{
    int to;
    ll cost;
};

int N; //頂点数
vector<edge> graph[200010];
ll dist[200010]; //頂点sからの最短距離
int pre[200010];

void Dijkstra(int s) {
    //greater<P>を指定することでfirstが小さい順に取り出せる
    priority_queue<P,vector<P>,greater<P>> que;
    fill(dist,dist + N,INF);
    fill(pre,pre + N,-1);
    dist[s] = 0;
    que.push(P(0,s));

    while(!que.empty()) {
        P p = que.top();
        que.pop();
        int v = p.second;
        if(dist[v] < p.first) {
            continue;
        }
        for(auto x:graph[v]) {
            if(chmin(dist[x.to],dist[v] + x.cost)) {
                pre[x.to] = v;
                que.push(P(dist[x.to],x.to));
            }
        }
    }
}

//最短路を取得
vector<int> get_path(int t) {
    vector<int> path;
    while(t != -1) {
        path.push_back(t);
        t = pre[t];
    }
    reverse(path.begin(),path.end());
    return path;
}

int main() {
    cin >> N;
    int M;
    cin >> M;
    for(int i = 0;i < M;i++) {
        int a,b;
        cin >> a >> b;
        a--;
        b--;
        graph[a].push_back(edge{b,1});
        graph[b].push_back(edge{a,1});
    }
    Dijkstra(0);
    if(dist[N - 1] == INF) cout << -1 << endl;
    else cout << dist[N - 1] << endl;
}
0