「Codeforces 938D」Buy a Ticket【最短路】

Time Limit: 2 Sec Memory Limit: 512 MB

Description

Musicians of a popular band “Flayer” have announced that they are going to “make their exit” with a world tour. Of course, they will visit Berland as well.

There are nn cities in Berland. People can travel between cities using two-directional train routes; there are exactly mm routes, the ii-th route can be used to go from city viv_i to city uiu_i (and from uiu_i to viv_i), and it costs wiw_i coins to use this route.

Each city will be visited by “Flayer”, and the cost of the concert ticket in the ii-th city is aia_i coins.

You have friends in every city of Berland, and they, knowing about your programming skills, asked you to calculate the minimum possible number of coins they have to pay to visit the concert. For every city ii you have to compute the minimum number of coins a person from city ii has to spend to travel to some city jj (or possibly stay in city ii), attend a concert there, and return to city ii (if jij \neq i).

Formally, for every i[1,n]i \in [1, n] you have to calculate minj=1n2d(i,j)+aj\displaystyle \min_{j=1}^n 2d(i, j) + a_j, where d(i, j) is the minimum number of coins you have to spend to travel from city ii to city jj. If there is no way to reach city jj from city ii, then we consider d(i, j) to be infinitely large.

Input

The first line contains two integers nn and mm (2n21052 \leqslant n \leqslant 2 \cdot 10^5, 1m21051 \leqslant m \leqslant 2 \cdot 10^5).

Then mm lines follow, the ii-th line contains three integers viv_i, uiu_i and wiw_i (1vi,uin1 \leqslant v_i, u_i \leqslant n, viuiv_i \neq u_i, 1wi10121 \leqslant w_i \leqslant 10^{12}) denoting the ii-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (v,u)(v, u) neither extra (v,u)(v, u) nor (u,v)(u, v) present in input.

The next line contains nn integers a1,a2,aka_1, a_2, \cdots a_k (1ai10121 \leqslant a_i \leqslant 10^{12}) — price to attend the concert in the ii-th city.

Output

Print nn integers. The ii-th of them must be equal to the minimum number of coins a person from city ii has to spend to travel to some city jj (or possibly stay in city ii), attend a concert there, and return to city ii (if jij \neq i).

Sample Input

1
2
3
4
5
6
7
8
9
10
11
12
Sample Input #1:
4 2
1 2 4
2 3 7
6 20 1 25

Sample Input #2:
3 3
1 2 1
2 3 1
1 3 1
30 10 20

Sample Output

1
2
3
4
5
Sample Output #1:
6 14 1 25

Sample Output #2:
12 10 12

Solution

建图方式如下:

  • 建立超级源点 00
  • 对于每条边 uiviu_i \rightarrow v_i,将距离乘以 22 后加入。
  • 对于权值 aia_i,从源点 00 向点 ii 连接一条权值为 aia_i 的边。

最后用 Dijkstra\text {Dijkstra} 算法从源点 00 运行一遍最短路即可,点 ii 的答案就是源点 00 到点 ii 的距离。

技巧:本题使用 priority_queue 会超时,需使用 set 才能通过,因为 set 的时间复杂度为 O(nlogn)O(n \cdot \log n) 且本题中的边数量较多。

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
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, int> P;
const int maxn = 2e5 + 10;
int n, m, u, v;
ll w, dist[maxn];
vector<P> G[maxn];
set<P> q;

int main() {
scanf("%d %d", &n, &m);
while (m--) {
scanf("%d %d %lld", &u, &v, &w);
G[u].push_back(P(2 * w, v));
G[v].push_back(P(2 * w, u));
}
for (int i = 1; i <= n; i++) {
scanf("%lld", &w);
G[0].push_back(P(w, i));
dist[i] = w + 1;
}
dist[0] = 0;
q.insert(P(0, 0));
while (q.size()) {
P p = *q.begin();
q.erase(q.begin());
for (auto e : G[p.second]) {
if (dist[e.second] > dist[p.second] + e.first) {
q.erase(P(dist[e.second], e.second)); // 重要!
dist[e.second] = dist[p.second] + e.first;
q.insert(P(dist[e.second], e.second));
}
}
}
for (int i = 1; i <= n; i++) {
printf("%lld ", dist[i]);
}
return 0;
}