package main import "fmt" /* Warchal_Floyd ワーシャル・フロイド すべての2点間の最短距離を求めるO(N^3) 1. d[N}[N]をMAXでうめる 2. d[i][i] = 0 3. k, i, j でまわす テスト用問題 */ const MAX = 10000*50*1000 + 1 type WarchalFloyd struct { n int r [][]int } func NewWarchalFloyd(n int) *WarchalFloyd { r := make([][]int, n) for i := range r { r[i] = make([]int, n) for j := 0; j < n; j++ { if i == j { r[i][j] = 0 } else { r[i][j] = MAX } } } return &WarchalFloyd{n: n, r: r} } func (wf *WarchalFloyd) conect(u, v, l int) { wf.r[u][v] = l } func (wf *WarchalFloyd) build() { for i := 0; i < wf.n; i++ { for j := 0; j < wf.n; j++ { for k := 0; k < wf.n; k++ { wf.r[i][j] = min(wf.r[i][j], wf.r[i][k]+wf.r[k][j]) } } } } func (wf *WarchalFloyd) cost(u, v int) int { return wf.r[u][v] } func main() { var N, M int fmt.Scan(&N) S := make([]int, N) for i := 0; i < N; i++ { fmt.Scan(&S[i]) } fmt.Scan(&M) wf := NewWarchalFloyd(N) var u, v, l int for i := 0; i < M; i++ { fmt.Scan(&u, &v, &l) wf.conect(u, v, l) wf.conect(v, u, l) } wf.build() ans := MAX for i := 1; i < N-1; i++ { for j := 1; j < N-1; j++ { if i == j { continue } ans = min(ans, wf.r[0][i]+wf.r[i][j]+wf.r[j][N-1]+S[i]+S[j]) } } fmt.Println(ans) } func min(a, b int) int { if a > b { return b } return a }