#pragma GCC optimize("O3,unroll-loops") #include #pragma GCC target("avx2,bmi2,popcnt,lzcnt") #include 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> using p_que = priority_queue, 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 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 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 inline constexpr bool is_pair_v = false; template inline constexpr bool is_pair_v> = 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 struct nyaan_static_modint : atcoder::static_modint { using base = atcoder::static_modint; 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 template 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 string to_pretty_str(T target) { string str; if constexpr (is_void_v) str += "void"s; else if constexpr (is_null_pointer_v) str += "null"s; else if constexpr (is_same_v) str += target ? "true"s : "false"s; else if constexpr (is_same_v || is_same_v || is_same_v || is_same_v) str += "'"s + target + "'"s; else if constexpr (atcoder::internal::is_modint::value) str += to_string(target.val()) + "(mod)"s; else if constexpr (is_arithmetic_v) { if constexpr (is_same_v) str += int128_to_str(target); else if (target >= INF) str += "INF"s; else if constexpr (is_signed_v) if (target <= -INF) str = "-INF"s; else str += to_string(target); } else if constexpr (is_pair_v) str += "("s + to_pretty_str(target.first) + ", "s + to_pretty_str(target.second) + ")"s; else if constexpr (is_convertible_v) str += "\""s + target + "\""s; else if constexpr (is_array_v) { 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(end(declval()))>) { str += "("s + get_typename(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(20) + " ("s + to_string(sizeof(target)) + " byte)>"s; return str; } template 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 void dump_f(string labels, tuple 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>>(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,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(n,false); in_s[n-1]=true; auto dfs=vector{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(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; } }