結果

問題 No.1565 Union
ユーザー simansiman
提出日時 2021-07-08 15:59:56
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,103 bytes
コンパイル時間 7,830 ms
コンパイル使用メモリ 108,168 KB
実行使用メモリ 10,388 KB
最終ジャッジ日時 2023-09-14 04:49:12
合計ジャッジ時間 7,502 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,892 KB
testcase_01 AC 3 ms
5,892 KB
testcase_02 AC 2 ms
5,904 KB
testcase_03 AC 3 ms
5,820 KB
testcase_04 AC 4 ms
5,820 KB
testcase_05 AC 4 ms
5,780 KB
testcase_06 AC 3 ms
5,820 KB
testcase_07 AC 3 ms
5,828 KB
testcase_08 AC 3 ms
5,828 KB
testcase_09 AC 3 ms
5,760 KB
testcase_10 AC 72 ms
8,180 KB
testcase_11 RE -
testcase_12 AC 141 ms
10,388 KB
testcase_13 AC 44 ms
7,192 KB
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
権限があれば一括ダウンロードができます

ソースコード

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 = 100101;

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