結果

問題 No.17 2つの地点に泊まりたい
ユーザー yukirinyukirin
提出日時 2016-03-04 23:42:16
言語 Go
(1.22.1)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,481 bytes
コンパイル時間 10,299 ms
コンパイル使用メモリ 237,552 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 02:10:59
合計ジャッジ時間 11,151 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1 ms
6,940 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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
}
0