思路
双倍经验。
显然,经过一条 且边权为 的边后,代价一定是 。而初始就经过 号点,所以 ,然后跑一遍 dijkstra 模板就行了。注意此题是双向边。
代码
# include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
struct node {
int x;
ll dis;
bool operator < (const node& t) const {
return dis > t.dis;
}
} t;
const ll inf = 1e18;
int n, m, a[200005], x, y, z;
ll d[200005];
vector <pii> v[200005];
priority_queue <node> q;
void dij () {
fill (d + 2, d + 1 + n, inf);
q.push ({1, d[1] = a[1]});
while (! q.empty ()) {
t = q.top ();
q.pop ();
if (t.dis > d[t.x])
continue ;
for (const pii& i : v[t.x])
if (d[i.first] > t.dis + i.second)
q.push ({i.first, d[i.first] = t.dis + i.second});
}
return ;
}
int main () {
ios::sync_with_stdio (0);
cin.tie (0);
cout.tie (0);
cin >> n >> m;
for (int i = 1; i <= n; ++ i)
cin >> a[i];
while (m --)
cin >> x >> y >> z, v[x].emplace_back (y, z + a[y]), v[y].emplace_back (x, z + a[x]);
dij ();
for (int i = 2; i <= n; ++ i)
cout << d[i] << ' ';
return 0;
}