#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 998244353
#define mxn 1000
#define inf 1000000000000000001ll
vector<pair<ll,ll>> g[100100];
int main()
{
ios::sync_with_stdio(0);
ll t = 1;
//cin >> t;
while(t--)
{
ll n, q;
cin >> n >> q;
ll a[n];
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < q; i++)
{
ll aq, bq, cq;
cin >> aq >> bq >> cq;
g[aq].push_back(make_pair(bq,abs(a[aq-1]-a[bq-1])));
g[aq].push_back(make_pair(cq,abs(a[aq-1]-a[cq-1])));
}
ll h[100100];
for(int i = 1; i <= n; i++)
h[i] = inf;
h[1] = 0;
set<pair<ll,ll> > s;
s.insert(make_pair(0,1));
while(s.size())
{
pair<ll, ll> bg = *s.begin();
s.erase(s.begin());
for(auto ch : g[bg.second])
{
if(h[ch.first]>bg.first+ch.second)
{
s.erase(make_pair(h[ch.first], ch.first));
h[ch.first] = bg.first+ch.second;
s.insert(make_pair(h[ch.first], ch.first));
}
}
}
for(int i = 1; i <= n; i++)
if(h[i]==inf)
cout << -1 << " ";
else
cout << h[i] << " ";
cout << endl;
}
return 0;
}
Copy