Treasure Hunt

시간 제한4초메모리 제한1024 MB

요약
각 정점에 값이 있는 가중 무방향 그래프에서 모든 시작 정점마다 (도착 정점의 값 - 경로 비용)의 최댓값을 구한다.
난이도

어려움10점 중 9점

유형
최단 경로, 그래프, 그리디, 수학
정답자
아직 제출이 없습니다

문제

Perry the Pirate is sailing the seven seas! He has a map consisting of NN islands connected by a network of MM sea routes. The ii-th sea route connects islands a_ia\_i and b_ib\_i and costs c_ic\_i coins to traverse in either direction. As it turns out, fighting off sea monsters can be quite expensive. In search of his next big plunder, Perry has scouted out each of the NN islands and has determined that the ii-th island contains a treasure chest with v_iv\_i coins inside.

It remains for him to plan out his next journey. He decides that he will sail through some (possibly empty) path of sea routes starting at island xx and ending at island yy. At the end of his journey, he will open the chest at island yy and collect his well-earned booty.

There is one small problem though: Perry doesn’t know what island he’s currently on! Thus, for every possible starting island xx, he would like to know the maximum possible number of coins he can earn out of all journeys starting at island xx. Can you help him compute these values? You may assume Perry has enough coins to traverse any path of sea routes he chooses; he only cares about the net profit of his next journey.

입력

The first line of input contains two space-separated integers NN and MM.

The second line of input contains NN space-separated integers v_1,v_2,…,v_Nv\_1, v\_2, \dots , v\_N (0≤v_i≤1090 ≤ v\_i ≤ 10^9).

The next MM lines each contain three space-separated integers a_ia\_i, b_ib\_i (1≤a_i,b_i≤N1 ≤ a\_i , b\_i ≤ N), and c_ic\_i (0≤c_i≤1090 ≤ c\_i ≤ 10^9).

It is guaranteed that there is at most one sea route between any pair of islands and each sea route connects two distinct islands.

출력

Output NN lines, where the xx-th line contains the maximum possible net profit (in coins) of any journey starting at island xx.

예제1

  1. 예제 1

    입력
    4 5
    6 5 9 2
    1 2 0
    3 2 3
    4 3 6
    1 3 5
    2 4 2
    
    예상 출력
    6
    6
    9
    4