結果

問題 No.1283 Extra Fee
ユーザー c-yanc-yan
提出日時 2020-11-11 00:23:10
言語 Go
(1.22.1)
結果
AC  
実行時間 900 ms / 2,000 ms
コード長 3,293 bytes
コンパイル時間 13,988 ms
コンパイル使用メモリ 235,612 KB
実行使用メモリ 24,600 KB
最終ジャッジ日時 2024-11-16 07:10:46
合計ジャッジ時間 21,782 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,820 KB
testcase_02 AC 1 ms
6,820 KB
testcase_03 AC 1 ms
6,820 KB
testcase_04 AC 2 ms
6,816 KB
testcase_05 AC 1 ms
6,816 KB
testcase_06 AC 1 ms
6,816 KB
testcase_07 AC 1 ms
6,820 KB
testcase_08 AC 2 ms
6,820 KB
testcase_09 AC 1 ms
6,820 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 17 ms
7,936 KB
testcase_12 AC 43 ms
8,060 KB
testcase_13 AC 10 ms
7,840 KB
testcase_14 AC 48 ms
9,944 KB
testcase_15 AC 93 ms
12,060 KB
testcase_16 AC 14 ms
8,084 KB
testcase_17 AC 186 ms
22,384 KB
testcase_18 AC 900 ms
23,128 KB
testcase_19 AC 590 ms
23,504 KB
testcase_20 AC 592 ms
24,048 KB
testcase_21 AC 534 ms
22,752 KB
testcase_22 AC 499 ms
24,600 KB
testcase_23 AC 72 ms
20,224 KB
testcase_24 AC 90 ms
21,888 KB
testcase_25 AC 579 ms
24,600 KB
testcase_26 AC 650 ms
24,036 KB
testcase_27 AC 585 ms
23,764 KB
testcase_28 AC 646 ms
23,752 KB
testcase_29 AC 214 ms
22,912 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.MaxInt64 >> 1
			dpb[i][j] = math.MaxInt64 >> 1
		}
	}
	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})
					}
				}
			} else {
				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})
					}
				}
			} else {
				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})
					}
				}
			} else {
				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})
					}
				}
			} else {
				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