結果

問題 No.1565 Union
ユーザー simansiman
提出日時 2021-07-08 16:00:57
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 1,103 bytes
コンパイル時間 4,765 ms
コンパイル使用メモリ 139,880 KB
実行使用メモリ 14,712 KB
最終ジャッジ日時 2024-05-05 13:25:53
合計ジャッジ時間 6,207 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,048 KB
testcase_01 AC 3 ms
8,192 KB
testcase_02 AC 4 ms
8,028 KB
testcase_03 AC 4 ms
8,064 KB
testcase_04 AC 5 ms
8,144 KB
testcase_05 AC 5 ms
8,100 KB
testcase_06 AC 3 ms
8,008 KB
testcase_07 AC 4 ms
8,152 KB
testcase_08 AC 3 ms
8,216 KB
testcase_09 AC 4 ms
8,232 KB
testcase_10 AC 68 ms
10,776 KB
testcase_11 AC 98 ms
12,128 KB
testcase_12 AC 115 ms
12,976 KB
testcase_13 AC 36 ms
9,408 KB
testcase_14 AC 133 ms
12,872 KB
testcase_15 AC 158 ms
13,804 KB
testcase_16 AC 155 ms
13,696 KB
testcase_17 AC 171 ms
14,712 KB
testcase_18 AC 179 ms
14,712 KB
testcase_19 AC 176 ms
14,712 KB
testcase_20 AC 129 ms
14,296 KB
testcase_21 AC 133 ms
14,336 KB
testcase_22 AC 128 ms
14,280 KB
testcase_23 AC 130 ms
14,404 KB
testcase_24 AC 133 ms
14,336 KB
testcase_25 AC 133 ms
14,292 KB
testcase_26 AC 126 ms
14,324 KB
testcase_27 AC 128 ms
14,560 KB
testcase_28 AC 134 ms
14,424 KB
testcase_29 AC 131 ms
14,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 200101;

struct Node {
  int v;
  int dist;

  Node(int v = -1, int dist = -1) {
    this->v = v;
    this->dist = dist;
  }

  bool operator>(const Node &n) const {
    return dist > n.dist;
  }
};

vector<int> E[MAX_N];

int main() {
  int N, M;
  cin >> N >> M;
  priority_queue <Node, vector<Node>, greater<Node>> pque;

  int a, b;
  for (int i = 0; i < M; ++i) {
    cin >> a >> b;
    E[a].push_back(b);
    E[b].push_back(a);
  }

  pque.push(Node(1, 0));
  vector<bool> visited(MAX_N, false);

  while (not pque.empty()) {
    Node node = pque.top();
    pque.pop();

    if (visited[node.v]) continue;
    visited[node.v] = true;

    if (node.v == N) {
      cout << node.dist << endl;
      return 0;
    }

    for (int u : E[node.v]) {
      pque.push(Node(u, node.dist + 1));
    }
  }

  cout << -1 << endl;

  return 0;
}
0