1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <bits/stdc++.h>
#define log printf #define EPS 1e-8 #define INF 0x3f3f3f3f3f3f3f3f #define FOR(i, l, r) for (ll(i) = (l); (i) <= (r); ++(i)) #define IOS \ ios::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr);
using namespace std;
typedef __int128 i128; typedef long long ll; typedef pair<ll, ll> PII;
const ll N = 1e6 + 10; ll m, n, s, x, y, z, a[N], w[N], to[N], idx, dis[N], nxt[N], head[N];
struct line { ll u, d; bool operator<(const line &cmp) const { return d > cmp.d; } }; priority_queue<line> q;
template <typename T>
inline T read() { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = sum * 10 + ch - '0'; return sum * fl; }
template <typename T>
inline void write(T x) { if (x < 0) putchar('-'), write<T>(-x); static T sta[35]; ll top = 0; do { sta[top++] = x % 10, x /= 10; } while (x); while (top) putchar(sta[--top] + 48); }
inline void init() { memset(head, -1, sizeof(head)); memset(dis, 0x3f, sizeof(dis)); }
inline void addEdge(ll u, ll v, ll q) { w[idx] = q; to[idx] = v; nxt[idx] = head[u]; head[u] = idx; idx++; }
void dijkstra(ll s) { dis[s] = 0; q.push({s, 0}); while (!q.empty()) { line f = q.top(); q.pop(); ll u = f.u; if (f.d > dis[u]) continue; for (ll i = head[u]; i != -1; i = nxt[i]) { ll v = to[i]; if (dis[u] + w[i] + a[v] < dis[v]) { dis[v] = dis[u] + w[i] + a[v]; q.push({v, dis[v]}); } } } }
int main() { init(); n = read<ll>(), m = read<ll>(), s = 1; FOR(i, 1, n) a[i] = read<ll>(); FOR(i, 1, m) { x = read<ll>(), y = read<ll>(), z = read<ll>(); addEdge(x, y, z), addEdge(y, x, z); } dijkstra(s); FOR(i, 2, n) write<ll>(dis[i] + a[1]), putchar(' '); return 0; }
|