結果

問題 No.1283 Extra Fee
ユーザー c-yanc-yan
提出日時 2020-11-11 00:15:25
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 3,219 bytes
コンパイル時間 13,227 ms
コンパイル使用メモリ 223,980 KB
実行使用メモリ 22,528 KB
最終ジャッジ日時 2024-07-22 18:15:11
合計ジャッジ時間 16,587 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 20 ms
5,888 KB
testcase_12 AC 48 ms
6,400 KB
testcase_13 AC 11 ms
5,376 KB
testcase_14 AC 55 ms
8,832 KB
testcase_15 AC 112 ms
11,008 KB
testcase_16 AC 17 ms
6,144 KB
testcase_17 AC 172 ms
21,632 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 92 ms
22,272 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 186 ms
22,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

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

func main() {
	defer flush()

	N := readInt()
	M := readInt()

	c := make([][]int, N)
	for i := 0; i < N; i++ {
		c[i] = make([]int, N)
	}

	for i := 0; i < M; i++ {
		h := readInt() - 1
		w := readInt() - 1
		c[h][w] = readInt()
	}

	dpa := make([][]int, N)
	dpb := make([][]int, N)
	for i := 0; i < N; i++ {
		dpa[i] = make([]int, N)
		dpb[i] = make([]int, N)
		for j := 0; j < N; j++ {
			dpa[i][j] = math.MaxInt32
			dpb[i][j] = math.MaxInt32
		}
	}
	dpa[0][0] = 0

	q := make([][3]int, 0)
	q = append(q, [3]int{0, 0, 0})
	for len(q) != 0 {
		y := q[0][0]
		x := q[0][1]
		t := q[0][2]
		q = q[1:]

		if y != 0 {
			if t == 0 {
				if dpa[y-1][x] > dpa[y][x]+1+c[y-1][x] {
					dpa[y-1][x] = dpa[y][x] + 1 + c[y-1][x]
					q = append(q, [3]int{y - 1, x, 0})
				}
				if c[y-1][x] > 0 {
					if dpb[y-1][x] > dpa[y][x]+1 {
						dpb[y-1][x] = dpa[y][x] + 1
						q = append(q, [3]int{y - 1, x, 1})
					}
				}
			}
			if dpb[y-1][x] > dpb[y][x]+1+c[y-1][x] {
				dpb[y-1][x] = dpb[y][x] + 1 + c[y-1][x]
				q = append(q, [3]int{y - 1, x, 1})
			}
		}

		if y != N-1 {
			if t == 0 {
				if dpa[y+1][x] > dpa[y][x]+1+c[y+1][x] {
					dpa[y+1][x] = dpa[y][x] + 1 + c[y+1][x]
					q = append(q, [3]int{y + 1, x, 0})
				}
				if c[y+1][x] > 0 {
					if dpb[y+1][x] > dpa[y][x]+1 {
						dpb[y+1][x] = dpa[y][x] + 1
						q = append(q, [3]int{y + 1, x, 1})
					}
				}
			}
			if dpb[y+1][x] > dpb[y][x]+1+c[y+1][x] {
				dpb[y+1][x] = dpb[y][x] + 1 + c[y+1][x]
				q = append(q, [3]int{y + 1, x, 1})
			}
		}

		if x != 0 {
			if t == 0 {
				if dpa[y][x-1] > dpa[y][x]+1+c[y][x-1] {
					dpa[y][x-1] = dpa[y][x] + 1 + c[y][x-1]
					q = append(q, [3]int{y, x - 1, 0})
				}
				if c[y][x-1] > 0 {
					if dpb[y][x-1] > dpa[y][x]+1 {
						dpb[y][x-1] = dpa[y][x] + 1
						q = append(q, [3]int{y, x - 1, 1})
					}
				}
			}
			if dpb[y][x-1] > dpb[y][x]+1+c[y][x-1] {
				dpb[y][x-1] = dpb[y][x] + 1 + c[y][x-1]
				q = append(q, [3]int{y, x - 1, 1})
			}
		}

		if x != N-1 {
			if t == 0 {
				if dpa[y][x+1] > dpa[y][x]+1+c[y][x+1] {
					dpa[y][x+1] = dpa[y][x] + 1 + c[y][x+1]
					q = append(q, [3]int{y, x + 1, 0})
				}
				if c[y][x+1] > 0 {
					if dpb[y][x+1] > dpa[y][x]+1 {
						dpb[y][x+1] = dpa[y][x] + 1
						q = append(q, [3]int{y, x + 1, 1})
					}
				}
			}
			if dpb[y][x+1] > dpb[y][x]+1+c[y][x+1] {
				dpb[y][x+1] = dpb[y][x] + 1 + c[y][x+1]
				q = append(q, [3]int{y, x + 1, 1})
			}
		}
	}
	println(min(dpa[N-1][N-1], dpb[N-1][N-1]))
}

const (
	ioBufferSize = 1 * 1024 * 1024 // 1 MB
)

var stdinScanner = func() *bufio.Scanner {
	result := bufio.NewScanner(os.Stdin)
	result.Buffer(make([]byte, ioBufferSize), ioBufferSize)
	result.Split(bufio.ScanWords)
	return result
}()

func readString() string {
	stdinScanner.Scan()
	return stdinScanner.Text()
}

func readInt() int {
	result, err := strconv.Atoi(readString())
	if err != nil {
		panic(err)
	}
	return result
}

var stdoutWriter = bufio.NewWriter(os.Stdout)

func flush() {
	stdoutWriter.Flush()
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdoutWriter, args...)
}
0