結果

問題 No.1473 おでぶなおばけさん
ユーザー kkishikkishi
提出日時 2021-04-09 21:44:34
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 9,002 bytes
コンパイル時間 2,870 ms
コンパイル使用メモリ 169,856 KB
実行使用メモリ 15,592 KB
最終ジャッジ日時 2024-06-25 04:50:14
合計ジャッジ時間 7,233 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 90 ms
14,572 KB
testcase_03 AC 60 ms
11,628 KB
testcase_04 AC 47 ms
9,576 KB
testcase_05 AC 17 ms
5,376 KB
testcase_06 AC 67 ms
11,876 KB
testcase_07 AC 85 ms
15,460 KB
testcase_08 AC 99 ms
15,460 KB
testcase_09 AC 93 ms
15,592 KB
testcase_10 AC 41 ms
6,372 KB
testcase_11 AC 41 ms
6,380 KB
testcase_12 AC 41 ms
6,248 KB
testcase_13 AC 28 ms
6,372 KB
testcase_14 AC 19 ms
5,376 KB
testcase_15 AC 36 ms
6,244 KB
testcase_16 AC 39 ms
6,504 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 7 ms
5,376 KB
testcase_19 AC 36 ms
6,372 KB
testcase_20 AC 53 ms
10,728 KB
testcase_21 AC 55 ms
8,936 KB
testcase_22 AC 83 ms
14,052 KB
testcase_23 AC 70 ms
12,392 KB
testcase_24 AC 69 ms
12,396 KB
testcase_25 AC 82 ms
13,544 KB
testcase_26 AC 89 ms
14,184 KB
testcase_27 AC 30 ms
7,140 KB
testcase_28 AC 89 ms
15,204 KB
testcase_29 AC 49 ms
8,428 KB
testcase_30 AC 58 ms
9,320 KB
testcase_31 AC 93 ms
15,340 KB
testcase_32 AC 55 ms
11,752 KB
testcase_33 AC 50 ms
10,724 KB
testcase_34 AC 29 ms
7,528 KB
testcase_35 AC 27 ms
6,508 KB
testcase_36 AC 58 ms
10,620 KB
testcase_37 AC 70 ms
11,876 KB
testcase_38 AC 13 ms
5,376 KB
testcase_39 AC 40 ms
6,164 KB
testcase_40 AC 39 ms
6,248 KB
testcase_41 AC 37 ms
6,248 KB
testcase_42 AC 37 ms
6,248 KB
testcase_43 AC 49 ms
12,008 KB
testcase_44 AC 50 ms
12,048 KB
testcase_45 AC 50 ms
12,008 KB
testcase_46 AC 58 ms
11,752 KB
testcase_47 AC 72 ms
13,420 KB
testcase_48 AC 64 ms
12,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

template <typename T>
class Graph {
 public:
  struct Edge {
    int from, to;
    T weight;
  };

  Graph(int n) : edges_(n) {}
  void AddEdge(int from, int to, T weight = T()) {
    edges_[from].push_back({from, to, weight});
  }
  const std::vector<Edge> &Edges(int from) const { return edges_[from]; }
  int NumVertices() const { return edges_.size(); }

 private:
  std::vector<std::vector<Edge>> edges_;
};

template <typename T>
std::vector<std::optional<T>> Dijkstra(const Graph<T>& graph, int start) {
  const int n = graph.NumVertices();

  std::vector<std::optional<T>> dist(n);

  using element = std::pair<T, int>;
  std::priority_queue<element, std::vector<element>, std::greater<>> que;

  auto push = [&dist, &que](int u, T c) {
    if (dist[u] && *dist[u] <= c) return;
    dist[u] = c;
    que.push({c, u});
  };

  push(start, 0);

  while (!que.empty()) {
    auto [c, u] = que.top();
    que.pop();
    if (c > dist[u]) continue;
    for (const auto& e : graph.Edges(u)) {
      push(e.to, c + e.weight);
    }
  }

  return dist;
}

class DisjointSet {
 public:
  DisjointSet(int size) : parent_(size), size_(size, 1) {
    for (int i = 0; i < size; ++i) {
      parent_[i] = i;
    }
  }
  int Find(int x) {
    int& p = parent_[x];
    if (p != x) p = Find(p);
    return p;
  }
  void Union(int x, int y) {
    x = Find(x), y = Find(y);
    if (x == y) return;
    if (Size(x) > Size(y)) std::swap(x, y);
    parent_[x] = parent_[y];
    size_[y] += size_[x];
  }
  int Size(int x) { return size_[Find(x)]; }
  bool Same(int x, int y) { return Find(x) == Find(y); }

 private:
  std::vector<int> parent_, size_;
};
#include <boost/hana/functional/fix.hpp>

template <typename T, typename = void>
struct is_dereferenceable : std::false_type {};
template <typename T>
struct is_dereferenceable<T, std::void_t<decltype(*std::declval<T>())>>
    : std::true_type {};

template <typename T, typename = void>
struct is_iterable : std::false_type {};
template <typename T>
struct is_iterable<T, std::void_t<decltype(std::begin(std::declval<T>())),
                                  decltype(std::end(std::declval<T>()))>>
    : std::true_type {};

template <typename T, typename = void>
struct is_applicable : std::false_type {};
template <typename T>
struct is_applicable<T, std::void_t<decltype(std::tuple_size<T>::value)>>
    : std::true_type {};

template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args);
template <typename T>
void debug(const T& v) {
  if constexpr (is_dereferenceable<T>::value) {
    std::cerr << "{";
    if (v) {
      debug(*v);
    } else {
      std::cerr << "nil";
    }
    std::cerr << "}";
  } else if constexpr (is_iterable<T>::value &&
                       !std::is_same<T, std::string>::value) {
    std::cerr << "{";
    for (auto it = std::begin(v); it != std::end(v); ++it) {
      if (it != std::begin(v)) std::cerr << ", ";
      debug(*it);
    }
    std::cerr << "}";
  } else if constexpr (is_applicable<T>::value) {
    std::cerr << "{";
    std::apply([](const auto&... args) { debug(args...); }, v);
    std::cerr << "}";
  } else {
    std::cerr << v;
  }
}
template <typename T, typename... Ts>
void debug(const T& value, const Ts&... args) {
  debug(value);
  std::cerr << ", ";
  debug(args...);
}
#if DEBUG
#define dbg(...)                        \
  do {                                  \
    cerr << #__VA_ARGS__ << ": ";       \
    debug(__VA_ARGS__);                 \
    cerr << " (L" << __LINE__ << ")\n"; \
  } while (0)
#else
#define dbg(...)
#endif

void read_from_cin() {}
template <typename T, typename... Ts>
void read_from_cin(T& value, Ts&... args) {
  std::cin >> value;
  read_from_cin(args...);
}
#define rd(type, ...) \
  type __VA_ARGS__;   \
  read_from_cin(__VA_ARGS__);
#define ints(...) rd(int, __VA_ARGS__);
#define strings(...) rd(string, __VA_ARGS__);

// Strings used for yes/no questions. Defined as variables so that it can be
// adjusted for each contest site.
const char *yes_str = "Yes", *no_str = "No";

template <typename T>
void write_to_cout(const T& value) {
  if constexpr (std::is_same<T, bool>::value) {
    std::cout << (value ? yes_str : no_str);
  } else if constexpr (is_iterable<T>::value &&
                       !std::is_same<T, std::string>::value) {
    for (auto it = std::begin(value); it != std::end(value); ++it) {
      if (it != std::begin(value)) std::cout << " ";
      std::cout << *it;
    }
  } else {
    std::cout << value;
  }
}
template <typename T, typename... Ts>
void write_to_cout(const T& value, const Ts&... args) {
  write_to_cout(value);
  std::cout << ' ';
  write_to_cout(args...);
}
#define wt(...)                 \
  do {                          \
    write_to_cout(__VA_ARGS__); \
    cout << '\n';               \
  } while (0)

#define all(x) (x).begin(), (x).end()
#define eb(...) emplace_back(__VA_ARGS__)
#define pb(...) push_back(__VA_ARGS__)

#define dispatch(_1, _2, _3, name, ...) name

#define as_i64(x)                                                          \
  (                                                                        \
      [] {                                                                 \
        static_assert(                                                     \
            std::is_integral<                                              \
                typename std::remove_reference<decltype(x)>::type>::value, \
            "rep macro supports std integral types only");                 \
      },                                                                   \
      static_cast<int64_t>(x))

#define rep3(i, a, b) for (int64_t i = as_i64(a); i < as_i64(b); ++i)
#define rep2(i, n) rep3(i, 0, n)
#define rep1(n) rep2(_loop_variable_, n)
#define rep(...) dispatch(__VA_ARGS__, rep3, rep2, rep1)(__VA_ARGS__)

#define rrep3(i, a, b) for (int64_t i = as_i64(b) - 1; i >= as_i64(a); --i)
#define rrep2(i, n) rrep3(i, 0, n)
#define rrep1(n) rrep2(_loop_variable_, n)
#define rrep(...) dispatch(__VA_ARGS__, rrep3, rrep2, rrep1)(__VA_ARGS__)

#define each3(k, v, c) for (auto&& [k, v] : c)
#define each2(e, c) for (auto&& e : c)
#define each(...) dispatch(__VA_ARGS__, each3, each2)(__VA_ARGS__)

template <typename T>
std::istream& operator>>(std::istream& is, std::vector<T>& v) {
  for (T& vi : v) is >> vi;
  return is;
}

template <typename T, typename U>
std::istream& operator>>(std::istream& is, std::pair<T, U>& p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T, typename U>
bool chmax(T& a, U b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T, typename U>
bool chmin(T& a, U b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T, typename U>
auto max(T a, U b) {
  return a > b ? a : b;
}

template <typename T, typename U>
auto min(T a, U b) {
  return a < b ? a : b;
}

template <typename T>
int64_t sz(const T& v) {
  return std::size(v);
}

template <typename T>
int64_t popcount(T i) {
  return std::bitset<std::numeric_limits<T>::digits>(i).count();
}

template <typename T>
bool hasbit(T s, int i) {
  return std::bitset<std::numeric_limits<T>::digits>(s)[i];
}

template <typename T, typename U>
auto div_floor(T n, U d) {
  if (d < 0) {
    n = -n;
    d = -d;
  }
  if (n < 0) {
    return -((-n + d - 1) / d);
  }
  return n / d;
};

template <typename T, typename U>
auto div_ceil(T n, U d) {
  if (d < 0) {
    n = -n;
    d = -d;
  }
  if (n < 0) {
    return -(-n / d);
  }
  return (n + d - 1) / d;
}

template <typename T>
bool even(T x) {
  return x % 2 == 0;
}

std::array<std::pair<int64_t, int64_t>, 4> adjacent(int64_t i, int64_t j) {
  return {{{i + 1, j}, {i, j + 1}, {i - 1, j}, {i, j - 1}}};
}

bool inside(int64_t i, int64_t j, int64_t I, int64_t J) {
  return 0 <= i && i < I && 0 <= j && j < J;
}

const int64_t big = std::numeric_limits<int64_t>::max() / 4;

using i64 = int64_t;
using i32 = int32_t;

template <typename T>
using low_priority_queue =
    std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <typename T>
using V = std::vector<T>;
template <typename T>
using VV = V<V<T>>;

void Main();

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);
  std::cout << std::fixed << std::setprecision(20);
  Main();
  return 0;
}

const auto& Fix = boost::hana::fix;

using namespace std;

#define int i64

void Main() {
  ints(n, m);
  V<tuple<int, int, int>> e;
  rep(m) {
    ints(s, t, d);
    --s, --t;
    e.eb(d, s, t);
  }
  sort(all(e), greater{});
  DisjointSet ds(n);
  Graph<int> g(n);
  int ans;
  rep(i, m) {
    auto [d, s, t] = e[i];
    ds.Union(s, t);
    g.AddEdge(s, t, 1);
    g.AddEdge(t, s, 1);
    ans = d;
    if (ds.Same(0, n - 1)) {
      while (++i < m) {
        auto [d, s, t] = e[i];
        if (d < ans) break;
        g.AddEdge(s, t, 1);
        g.AddEdge(t, s, 1);
      }
      break;
    }
  }
  wt(ans, *Dijkstra(g, 0)[n - 1]);
}
0