Untitled
#pragma GCC optimize "O2" #include <bits/stdc++.h> using namespace std; #define ve vector #define pb push_back #define all(a) a.begin(), a.end() using ll = long long; using ld = long double; #define F first #define S second #define int ll using pll = pair<int, int>; const int inf = 1e18; template<class A> istream &operator>>(istream &in, ve<A> &a) { for (auto &x : a) in >> x; return in; } template<class A> ostream &operator<<(ostream &out, ve<A> a) { for (auto &x : a) out << x << ' '; return out << '\n'; } int32_t main() { #ifdef DEBUG_Q freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; ve<int> r(n); for (auto &x : r) cin >> x; ve<ve<pll>> g(n); for (int i = 0; i < m; ++i) { int u, v, w; cin >> u >> v >> w; g[u-1].pb({v-1, w}); g[v-1].pb({u-1, w}); } ve<int> ind(n); iota(all(ind), 0); sort(all(ind), [&](int i, int j) -> bool { return r[i] > r[j]; }); ve<int> used(n), used2(n); ve<int> dist(n, inf); ve<int> nw_dist(n, inf); int pred = -1; int timer = 0; ve<int> b(n); ve<pll> changes; for (auto &i : ind) { if (r[i] != r[pred]) { while (!changes.empty()) { auto [d, v] = changes.back(); dist[v] = min(dist[v], d); changes.pop_back(); } pred = i; } priority_queue<pll ,ve<pll>, greater<>> pq; pq.push({0, i}); timer++; while (!pq.empty()) { auto [d, v] = pq.top(); pq.pop(); if (used[v] == timer) continue; b[i]++; used[v] = timer; changes.pb({d, v}); for (auto &&[u, w] : g[v]) { if (d + w < dist[u] && (used2[u] != timer || nw_dist[u] > d + w)) { used2[u] = timer; nw_dist[u] = d + w; pq.push({d + w, u}); } } } } int ans = 0; for (auto &x : b) ans += x; cout << ans; }
Leave a Comment