結果

問題 No.1 道のショートカット
ユーザー warashiwarashi
提出日時 2016-01-15 09:50:29
言語 Go
(1.23.4)
結果
AC  
実行時間 22 ms / 5,000 ms
コード長 1,172 bytes
コンパイル時間 13,628 ms
コンパイル使用メモリ 226,416 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-20 16:19:37
合計ジャッジ時間 13,200 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
)

const inf = 1 << 28

type e struct {
	v, c, t int
}

func scanIntSet(N int) []int {
	r := make([]int, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&r[i])
	}
	return r
}

func min(x, y int) int {
	if y < x {
		return y
	}
	return x
}

func main() {
	var N, C, V int
	fmt.Scan(&N, &C, &V)
	S := scanIntSet(V)
	T := scanIntSet(V)
	Y := scanIntSet(V)
	M := scanIntSet(V)

	E := make(map[int][]e)
	for i := 0; i < V; i++ {
		from, to, cost, time := S[i], T[i], Y[i], M[i]
		E[from] = append(E[from], e{v: to, c: cost, t: time})
	}

	// path[町の番号][そこまでのコスト] = そのpathの時間
	path := make([][]int, N+1)
	for i := 1; i <= N; i++ {
		path[i] = make([]int, C+1)
		for j := 0; j <= C; j++ {
			path[i][j] = inf
		}
	}
	path[1][0] = 0

	for i := 1; i <= N; i++ {
		for c0 := 0; c0 <= C; c0++ {
			if path[i][c0] == inf {
				continue
			}
			for _, e := range E[i] {
				if c0+e.c > C {
					continue
				}
				path[e.v][c0+e.c] = min(path[e.v][c0+e.c], path[i][c0]+e.t)
			}
		}
	}
	goal := inf
	for i := 0; i <= C; i++ {
		goal = min(goal, path[N][i])
	}
	if goal == inf {
		fmt.Println(-1)
		return
	}
	fmt.Println(goal)
}
0