「CF938D」Buy a Ticket - 最短路

一个裸的最短路.....Orz这种套路的题都不会做了

Link: Problem - 938D - Codeforces

简要题意

给定 n 个点, m 条边的无向图, 点有点权, 边有边权, \forall u , 求

\min _ {v \in V} (2 \times \text {dist}(u, v) + w_v)

其中 \text{dist}(u, v) 代表两点最短路, w_v 表示 v 点的点权

Buy a Ticket

time limit per test 2 seconds
memory limit per test 256 megabytes
input standard input
output standard output

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 n cities in Berland. People can travel between cities using two-directional train routes; there are exactly m routes, i -th route can be used to go from city v_i to city u_i (and from u_i to v_i ), and it costs wi coins to use this route.

Each city will be visited by "Flayer", and the cost of the concert ticket in i -th city is a_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 i you have to compute the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i ), attend a concert there, and return to city i (if j \neq i ).

Formally, for every you have to calculate , where d(i, j) is the minimum number of coins you have to spend to travel from city i to city j. If there is no way to reach city j from city i, then we consider d(i, j) to be infinitely large.

Input

The first line contains two integers n and m (2 ≤ n ≤ 2·105, 1 ≤ m ≤ 2·105).

Then m lines follow, i-th contains three integers vi, ui and wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 1 ≤ wi ≤ 1012) denoting i-th train route. There are no multiple train routes connecting the same pair of cities, that is, for each (v, u) neither extra (v, u) nor (u, v) present in input.

The next line contains n integers a1, a2, ... ak (1 ≤ ai ≤ 1012) — price to attend the concert in i-th city.

Output

Print n integers. i-th of them must be equal to the minimum number of coins a person from city i has to spend to travel to some city j (or possibly stay in city i), attend a concert there, and return to city i (if j ≠ i).

Examples

Input

1
2
3
4
4 2
1 2 4
2 3 7
6 20 1 25

Output

1
6 14 1 25

Input

1
2
3
4
5
3 3
1 2 1
2 3 1
1 3 1
30 10 20

Output

1
12 10 12

题解

首先 \text{dist} 的系数 2 是可以弄掉的, 不考虑这个东西, 于是式子就变成了

\min _ {v \in V} (\text {dist}(u, v) + w_v)

w_v 直接处理较难, 于是转化问题, 把 w_v 转到边上(全是边权显然比边权点权混合好考虑):

新建一个点 \text S , 从 \text S 向每个点 i 连一条长度为 w_i 的边, 于是要求的内容变成了

\min _ {v \in V} (\text {dist} (u, v) + \text {dist(S}, v))

然后就大功告成了, 上面这个式子显然是 \text S \to v 的最短路, 于是跑一边最短路就好了. 另外注意不要爆 int

Code

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

#ifndef __HEADERS__
#define __HEADERS__

#define up(i,j,k) for (int i = j; i <= k; i++)
#define down(i,j,k) for (int i = j; i >= k; i--)

#define lb long double
#define ll long long
#define ull unsigned long long

#define mp std::make_pair
#define pb push_back

#define all(x) x.begin(), x.end()
#define lowbit(x) ((x) & (-x))
#define mem(a) memset(a, 0, sizeof(a))
#define File(x) \
freopen("" #x ".in", "r", stdin); \
freopen("" #x ".out", "w", stdout);

#endif

#define int long long

inline int read() {
int data = 0, w = 1; char ch = 0;
while (ch != '-' && (ch > '9' || ch < '0')) ch = getchar(); if (ch == '-') w = -1, ch = getchar();
while (ch >= '0' && ch <= '9') { data = data * 10 + ch - '0'; ch = getchar(); }
return data * w;
}

inline void print (ll v) {
if (v < 0) { putchar('-'); v = -v; }
if (v > 9) print(v / 10);
putchar(v % 10 + '0');
}

const int N = 200000 + 10;

int n, m;
std::vector <std::pair<int, int> > v[N];

void addEdge (int from, int to, int dist) { v[from].pb(mp(to, dist)); }

int dist[N], vis[N];
void dij () {
std::priority_queue <std::pair<int, int> > q;
memset(dist, 0x3f, sizeof dist);
q.push(mp(0, n + 1)); dist[n + 1] = 0;
while (!q.empty()) {
int from = q.top().second; q.pop();
if (vis[from]) continue;
vis[from] = 1;
for (auto i : v[from]) {
if (dist[i.first] > dist[from] + i.second) {
dist[i.first] = dist[from] + i.second;
q.push(mp(-dist[i.first], i.first));
}
}
}
}

signed main () {
n = read(); m = read();
int u, v, w;
up (i, 1, m) {
u = read(); v = read(); w = read() * 2;
addEdge (u, v, w); addEdge (v, u, w);
}
up (i, 1, n) addEdge (n + 1, i, read());
dij();
up (i, 1, n) print(dist[i]), putchar(' ');
return 0;
}
坚持原创技术分享,您的支持将鼓励我继续创作!