結果
| 問題 | No.17 2つの地点に泊まりたい | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2016-03-04 23:42:16 | 
| 言語 | Go (1.23.4) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,481 bytes | 
| コンパイル時間 | 15,796 ms | 
| コンパイル使用メモリ | 227,336 KB | 
| 実行使用メモリ | 5,248 KB | 
| 最終ジャッジ日時 | 2024-10-15 01:30:44 | 
| 合計ジャッジ時間 | 14,632 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 27 | 
ソースコード
package main
import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)
var sc = bufio.NewScanner(os.Stdin)
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)
func main() {
	sc.Split(bufio.ScanWords)
	n := nextInt()
	ns := make([]int, n)
	for i := range ns {
		ns[i] = nextInt()
	}
	t := make([][]int, n)
	for i := range t {
		t[i] = make([]int, n)
		for j := range t[i] {
			t[i][j] = 1e15
		}
		t[i][i] = 0
	}
	m := nextInt()
	for i := 0; i < m; i++ {
		a, b, c := nextInt(), nextInt(), nextInt()
		t[a][b], t[b][a] = c, c
	}
	t, _ = search(t)
	dp := make([]int, 3000)
	dp[0] = -1
	for i := 0; i < 2; i++ {
		next := make([]int, 3000)
		for j, v := range dp {
			if v == 0 {
				continue
			}
			for k := 1; k < n-1; k++ {
				if j == k {
					continue
				}
				next[j*n+k] = v + t[j][k] + ns[k]
			}
		}
		dp = next
	}
	min := int(1e15)
	for i, v := range dp {
		if v < 1 {
			continue
		}
		if v+t[i%n][n-1] < min {
			min = v + t[i%n][n-1]
		}
	}
	fmt.Println(min + 1)
}
func nextLine() string {
	sc.Scan()
	return sc.Text()
}
func nextInt() int {
	i, _ := strconv.Atoi(nextLine())
	return i
}
func search(dp [][]int) ([][]int, error) {
	for k := 0; k < len(dp); k++ {
		for i := 0; i < len(dp); i++ {
			for j := 0; j < len(dp); j++ {
				cost := dp[i][j]
				other := dp[i][k] + dp[k][j]
				if other < cost {
					cost = other
				}
				dp[i][j] = cost
			}
		}
	}
	for i := range dp {
		if dp[i][i] < 0 {
			return dp, fmt.Errorf("negative cycle")
		}
	}
	return dp, nil
}
            
            
            
        