#include using namespace std; using ll = long long; const int inf = 1 << 30; const ll INF = 1LL << 60; const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1}; #define rep(i, s, t) for (ll i = (ll)s; i < (ll)(t); i++) #define rrep(i, s, t) for (ll i = (ll)(t) - 1; i >= (ll)(s); i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmin(T1& x, T2 y) { return x > y ? (x = y, true) : false; } template bool chmax(T1& x, T2 y) { return x < y ? (x = y, true) : false; } template using min_heap = priority_queue, greater>; template using max_heap = priority_queue; void solve() { int N, M; cin >> N >> M; vector>> G(N); rep(i, 0, M) { int a, b; ll c; cin >> a >> b >> c; a--, b--; G[a].push_back({b, c}); G[b].push_back({a, c}); } auto calc_dist = [&](int s) -> vector { vector dist(N, INF); dist[s] = 0; min_heap> pq; pq.push({0, s}); while (pq.size()) { auto [c, x] = pq.top(); pq.pop(); if (dist[x] < c) continue; for (auto [i, e] : G[x]) { if (chmin(dist[i], c + e)) pq.push({dist[i], i}); } } return dist; }; vector distF = calc_dist(0), distB = calc_dist(N - 1); vector seen(N); int now = N - 1; ll tm = 0; vector> vp; seen[N - 1] = true; while (1) { int to = -1; ll mi = INF; for (auto [i, e] : G[now]) { if (chmin(mi, e)) to = i; } if (!seen[to]) { seen[to] = true; vp.push_back({now, to, tm, tm + mi}); tm += mi; now = to; } else { vp.push_back({now, to, tm, tm + mi}); break; } } ll opt = distF[N - 1]; rep(i, 0, (int)vp.size()) { auto [u, v, s, t] = vp[i]; ll e = t - s; if (i < (int)vp.size() - 2) { if (s - e <= distF[v] && distF[v] <= t && distF[v] + e + distB[u] == opt) { cout << "Yes\n"; return; } } else { if (s - e <= distF[v] && distF[v] + e + distB[u] == opt) { cout << "Yes\n"; return; } } } rep(i, 0, (int)vp.size()) { auto [u, v, s, t] = vp[i]; ll e = t - s; if (i < (int)vp.size() - 2) { if (distF[u] == s && distF[u] + distB[u] == opt) { cout << "Yes\n"; return; } } else { if (distF[u] >= s && (distF[u] - s) % (2 * e) == 0 && distF[u] + distB[u] == opt) { cout << "Yes\n"; return; } } } cout << "No\n"; } int main() { cin.tie(0)->sync_with_stdio(0); cout << fixed << setprecision(15); srand(time(NULL)); int T; // cin >> T; T = 1; while (T--) solve(); }