結果

問題 No.3833 Encounter
コンテスト
ユーザー りすりす/TwoSquirrels
提出日時 2026-11-28 00:23:25
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 6,801 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 6,860 ms
コンパイル使用メモリ 428,920 KB
実行使用メモリ 25,140 KB
最終ジャッジ日時 2026-11-28 00:23:42
合計ジャッジ時間 15,293 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 28 WA * 15 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
#pragma GCC target("avx2,bmi2,popcnt,lzcnt")
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
const ll INF = 4e18;
using ld = long double;
const ld INFD = INFINITY;
const ld EPS = LDBL_EPSILON;
template<class T, class Comp = less<T>> using p_que = priority_queue<T, vector<T>, Comp>;
array sin90 = { 0, 1, 0, -1 }, cos90 = { 1, 0, -1, 0 };
array sin45 = { 0, 1, 1, 1, 0, -1, -1, -1 }, cos45 = { 1, 1, 0, -1, -1, -1, 0, 1 };
#define p1 first
#define p2 second
#define reps(i, l, r) for (decay_t<decltype(r)> i##_right = (r), i = (l); i < i##_right; ++i)
#define rep(i, n) reps(i, 0, n)
#define rreps(i, l, r) for (decay_t<decltype(r)> i##_left = (l), i = (r) - 1; i >= i##_left; --i)
#define rrep(i, n) rreps(i, 0, n)
#define all(a) begin(a), end(a)
#define sz(a) ll(ssize(a))
template <typename T> inline constexpr bool is_pair_v = false;
template <typename T, typename U> inline constexpr bool is_pair_v<pair<T, U>> = true;
bool chmin(auto& a, auto b) { return a > b ? a = b, 1 : 0; }
bool chmax(auto& a, auto b) { return a < b ? a = b, 1 : 0; }
string int128_to_str(__int128_t target) {
  string target_str;
  __uint128_t target_tmp = target < 0 ? -target : target;
  do { target_str += '0' + target_tmp % 10; target_tmp /= 10; } while (target_tmp != 0);
  if (target < 0) target_str += '-';
  reverse(target_str.begin(), target_str.end());
  return target_str;
}
template<int m> struct nyaan_static_modint : atcoder::static_modint<m> {
  using base = atcoder::static_modint<m>;
  using base::base;
  nyaan_static_modint() = default;
  nyaan_static_modint(const base &x) : base(x) {}
  static constexpr unsigned int get_mod() { return m; }
  unsigned int get() const { return base::val(); }
  nyaan_static_modint inverse() const { return base::inv(); }
};
#ifdef DEBUG
#include <cxxabi.h>
template <typename T> inline string get_typename(size_t length_limit = string::npos) {
  string name = abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, nullptr);
  if (name.length() > length_limit) name = name.substr(0, length_limit - 3) + "...";
  return name;
}
template <typename T> string to_pretty_str(T target) {
  string str;
  if constexpr (is_void_v<T>) str += "void"s;
  else if constexpr (is_null_pointer_v<T>) str += "null"s;
  else if constexpr (is_same_v<T, bool>) str += target ? "true"s : "false"s;
  else if constexpr (is_same_v<T, char> || is_same_v<T, char16_t> || is_same_v<T, char32_t> || is_same_v<T, wchar_t>) str += "'"s + target + "'"s;
  else if constexpr (atcoder::internal::is_modint<T>::value) str += to_string(target.val()) + "(mod)"s;
  else if constexpr (is_arithmetic_v<T>) {
    if constexpr (is_same_v<T, __int128_t>) str += int128_to_str(target);
    else if (target >= INF) str += "INF"s;
    else if constexpr (is_signed_v<T>) if (target <= -INF) str = "-INF"s;
    else str += to_string(target);
  } else if constexpr (is_pair_v<T>) str += "("s + to_pretty_str(target.first) + ", "s + to_pretty_str(target.second) + ")"s;
  else if constexpr (is_convertible_v<T, string>) str += "\""s + target + "\""s;
  else if constexpr (is_array_v<T>) {
    str += "["s;
    bool separate = false;
    for (const auto &target_i : target) { if (separate) str += ", "s; str += to_pretty_str(target_i); separate = true; }
    str += "]"s;
  } else if constexpr (is_same_v<decltype(begin(declval<T>())), decltype(end(declval<T>()))>) {
    str += "("s + get_typename<T>(20) + "){"s;
    bool separate = false;
    for (const auto &target_i : target) { if (separate) str += ","s; str += " "s + to_pretty_str(target_i); separate = true; }
    if (separate) str += " "s;
    str += "}"s;
  } else str += "<"s + get_typename<T>(20) + " ("s + to_string(sizeof(target)) + " byte)>"s;
  return str;
}
template <typename F> void debug_txt_f(F callback, int line = -1, string file = (__FILE__)) {
  string dump_str = "[DEBUG] ";
  if (line >= 0) {
    dump_str += "(";
    if (file != (__FILE__)) dump_str += file + ":";
    dump_str += "L" + to_string(line) + ") ";
  }
  dump_str += callback();
  cerr << dump_str << endl;
}
#  define debug_txt(txt) (debug_txt_f([&]() { return to_pretty_str(txt); }, (__LINE__), (__FILE__)), true)
template <typename... Types>
void dump_f(string labels, tuple<Types...> targets_tupl, int line = -1, string file = (__FILE__)) {
  debug_txt_f([=]() {
    string txt;
    apply([labels, &txt](auto... targets) {
      int i = 0, label_left = 0;
      (([&](auto target) {
        const auto label_len = labels.find(',', label_left) - label_left;
        if (i >= 1) txt += ", ";
        txt += regex_replace(labels.substr(label_left, label_len), regex("^\\s+|\\s+$"), "") + ": " + to_pretty_str(target);
        label_left += label_len + 1;
        ++i;
      })(targets), ...);
      txt += ";";
    }, targets_tupl);
    return txt;
  }, line, file);
}
#  define dump(...) (dump_f((#__VA_ARGS__), make_tuple(__VA_ARGS__), (__LINE__), (__FILE__)), true)
#else
#  define debug_txt(...) (false)
#  define dump(...) (false)
#endif

// ================================ ANSWER ================================

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(12);

  ll n,m;
  cin>>n>>m;
  auto g=vector<vector<pair<ll,ll>>>(n);
  rep(i,m) {
    ll a,b,c;
    cin>>a>>b>>c;
    --a,--b;
    g[a].emplace_back(c,b);
    g[b].emplace_back(c,a);
  }
  rep(u,n) {
    sort(all(g[u]));
  }
  dump(g);

  auto cost=vector(n,INF);
  cost[0]=0;
  auto pq=p_que<pair<ll,ll>,greater<>>();
  pq.emplace(0,0);
  while (!pq.empty()) {
    auto [uc,u]=pq.top();
    pq.pop();
    if (uc>cost[u]) continue;
    for (auto [w,v]:g[u]) {
      if (chmin(cost[v],cost[u]+w)) pq.emplace(cost[v],v);
    }
  }
  dump(cost);

  auto in_s=vector<int>(n,false);
  in_s[n-1]=true;
  auto dfs=vector<ll>{n-1};
  while (!dfs.empty()) {
    auto v=dfs.back();
    dfs.pop_back();
    for (auto [w,u]:g[v]) {
      if (cost[u]!=cost[v]-w) continue;
      if (in_s[u]) continue;
      in_s[u]=true;
      dfs.emplace_back(v);
    }
  }
  dump(in_s);

  auto in_t=vector<int>(n,false);
  in_t[n-1]=true;
  auto u=n-1;
  auto c=0LL;
  while (true) {
    auto [w,v]=g[u][0];
    if (in_s[u]&&cost[u]==c) {
      cout<<"Yes\n";
      return 0;
    }
    if (in_s[u]&&in_s[v]&&cost[v]+w==cost[u]) {
      if (c<=cost[u]&&c+w>=cost[v]) {
        cout<<"Yes\n";
        return 0;
      }
    }
    in_t[u]=true;
    if (in_t[v]) {
      if (in_s[u]&&in_s[v]&&cost[v]+w==cost[u]) cout<<"Yes\n";
      else if (in_s[u]&&cost[u]>=c&&(cost[u]-c)%w==0) cout<<"Yes\n";
      else if (in_s[v]&&cost[v]>=c+w&&(cost[v]-c)%w==0) cout<<"Yes\n";
      else cout<<"No\n";
      return 0;
    }
    u=v;
    c+=w;
  }
}
0